1.JavaScript Statements
JavaScript statements are commands that are sent to the browser.
The purpose of these commands is to tell the browser what to do.
The following JavaScript statement outputs the text "Hello Dolly" to the HTML element of id= "demo"
document.getElementById ("demo"" hello Dolly";
1.1 Semicolon;
Semicolons are used to separate JavaScript statements.
Usually we add semicolons at the end of each executable statement.
Another useful use of semicolons is to write multiple statements in one line.
6= a +56; c = a + B;
1.2JavaScript Code
JavaScript code is a sequence of javascript statements.
The browser executes each statement sequentially, in the order in which it was written.
This example outputs a title and two paragraphs to a Web page:
document.getElementById ("demo"). Innerhtml=" hello Dolly" ;d Ocument.getelementbyid ("mydiv"). Innerhtml=" How have you been? ";
1.3JavaScript code block
JavaScript can be grouped together in batches.
The code block begins with an opening curly brace and ends with a closing curly brace.
The function of a code block is to execute a sequence of statements together.
This example outputs a title and two paragraphs to a Web page:
function myFunction () { document.getElementById ("Demo"). innerhtml= "Hello Dolly"; document.getElementById ("mydiv"). Innerhtml= "How have you been?" ;}
1.4JavaScript Statement Identifier
JavaScript statements typically begin with a statement identifier and execute the statement.
Statement identifiers are reserved keywords that cannot be used as variable names.
The following table lists the JavaScript statement identifiers (keywords):
1.5 spaces
JavaScript ignores extra spaces. You can add spaces to the script to improve its readability. The following two lines of code are equivalent:
var person= "Hege";
var person = "Hege";
2.JavaScript Comments
JavaScript does not execute annotations.
We can add comments to explain JavaScript, or to improve the readability of the code.
Single-line comments begin with//.
This example uses a single-line comment to interpret the code:
// Output title:document.getElementById ("myH1"). Innerhtml= "Welcome to my homepage"; // Output paragraph:document.getElementById ("MyP"). Innerhtml= "This is my first paragraph. ";
2.1JavaScript Multi-line Comment
Multiline comments start with/* and end with */.
The following example uses a multiline comment to interpret the code:
/* The following code will output a title and a paragraph and will represent the beginning of the home page */ document.getElementById ("myH1"). Innerhtml= "Welcome to my Homepage";d Ocument.getelementbyid ("MyP"). Innerhtml= "This is my first paragraph. ";
2.2 Using annotations to block execution
In the following example, a comment is used to block the execution of one of the lines of code (available for debugging):
// document.getElementById ("myH1"). Innerhtml= "Welcome to my homepage"; document.getElementById ("MyP"). Innerhtml= "This is my first paragraph. ";
In the following example, a comment is used to block the execution of a block of code (available for debugging):
/* document.getElementById ("myH1"). Innerhtml= "Welcome to my Homepage";d Ocument.getelementbyid ("MyP"). Innerhtml= "This is my first paragraph. "; */
2.3 Using annotations at the end of a line
In the following example, we put the comment at the end of the line of code:
var x=5; // declare x and assign the value 5 to it . var y=x+2; // declare y and assign the x+2 to it .
JavaScript use (ii)