<script> ... </script> element we write our JS code.
In the '.js'
external file we can't use HTML tags.
instruction is used to print something on the page.
• If you want to load the script from an external file (eg, 'code.js
'), our code in the HTML document will look like:
<h4>Example simple JS code</h4>
- The 'code.js' contains:
In any language, the code has conventions and rules of syntax.
- Case-sensitive - It makes distinction between large and small letters, such words as 'examples, Examples' will be treated differently.
- Semicolon (;) - All line statements must end with a semicolon character (;) (Example' var1 = 3; var2 = 8;).
var1 = 'JS tutorial';
//is the same as
- Special Characters - When we write scripts, sometimes we need to use a special character in values or in data output, or a key press such as the TAB key, or a new line. In that case we must use the backslash character '\' in front of one of the Escape codes:
- \b - backspace
\f - new page
\n - new line
\r - indicate a carriage return
\t - indicate a pressing TAB key
\\ - a backslash character
\' - indicate an apostrophe (single quotes)
\" - double quotes
- - For example, if you want to display a text using
- Comments - The comments within the code are necessary when we want to specify the role and functions of certain variables or instructions, for easy understanding of the script later.
To add a comment on a single line, inside the code, use double slash //. All characters to the right of the double slash represent a comment.
// Comment on a single line
var str ='String';
If you want to write comments on multiple lines, use /* at the beginning of the comment, and */ at its end.
/* comment on ...
end line */
var str ='String';
- The name of the variables and functions - The name given to variables and functions must follow the following rules:
- - The first character must be a letter, an underscore (_) or $ sign.
- The first character can not be a number.
- the name should not contain blank space.
Daily Test with Code Example
Which HTML5 tag is indicated to be used as container for menu with navigation links in Web site? <section> <nav> <article>
<li><a href="http://coursesweb.net/css/" title="CSS Course">CSS Course</a></li>
<li><a href="http://www.marplo.net/jocuri/" title="Flash Games">Flash Games</a></li>
Which CSS property shifts an item horizontally to the left or right of where it was? text-align clear float
Click on the Math object method which returns x, rounded downwards to the nearest integer. Math.ceil(x) Math.abs(x) Math.floor(x)
var num = 12.34567;
num = Math.floor(num);
alert(num); // 12
Indicate the PHP function which returns the number of characters in string. mb_strlen() count() stristr()
$str = "string with utf-8 chars åèö";
$nrchr = mb_strlen($str);
echo $nrchr; // 30