<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.