<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 defines marked text? (can be used to highlight parts of text) <mark> <embed> <span>
<p>Free corses: <mark>coursesweb.net</mark> for Web Development.</p>
Which CSS pseudo-class adds a style to an element when the mouse is over it? :focus :hover :active
Click on the function which returns a string value that represents the number rounded to the x digits after the decimal point. toPrecision(x) toFixed(x) floor(x)
var num = 12.34567;
num = num.toFixed(2);
alert(num); // 12.35
Indicate the PHP function which reads an entire file into an array. [) file() readfile()
$arr = file("a_file.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);