JAVASCRIPT STATEMENTS AND COMMENTS

View previous topic View next topic Go down

JAVASCRIPT STATEMENTS AND COMMENTS

Post by Admin on Tue Jul 28, 2015 6:36 pm

In HTML, JavaScript statements are "instructions" to be "executed" by the web browser.


JavaScript Statements

This statement tells the browser to write "Hello Dolly." inside an HTML element with id="demo":

Example


Code:
document.getElementById("demo").innerHTML = "Hello Dolly.";






JavaScript Programs

Most JavaScript programs contain many JavaScript statements.
The statements are executed, one by one, in the same order as they are written.
In this example, x, y, and z is given values, and finally z is displayed:

Example



Code:
var x = 5;
var y = 6;
var z = x + y;
document.getElementById("demo").innerHTML = z;
[size]
Remember to put the html and script tags before putting the code.


[/size]

[th][/th]
JavaScript programs (and JavaScript statements) are often called JavaScript code.



Semicolons ;

Semicolons separate JavaScript statements.
Add a semicolon at the end of each executable statement:
a = 5;
b = 6;
c = a + b;


When separated by semicolons, multiple statements on one line are allowed:
a = 5; b = 6; c = a + b;



[th][/th]
On the web, you might see examples without semicolons. 
Ending statements with semicolon is not required, but highly recommended.



JavaScript White Space

JavaScript ignores multiple spaces. You can add white space to your script to make it more readable.
The following lines are equivalent:
var person = "Hege";
var person="Hege";



JavaScript Line Length and Line Breaks

For best readability, programmers often like to avoid code lines longer than 80 characters.
If a JavaScript statement does not fit on one line, the best place to break it, is after an operator:

Example



Code:
document.getElementById("demo").innerHTML =
"Hello Dolly.";







JavaScript Code Blocks

JavaScript statements can be grouped together in code blocks, inside curly brackets {...}.
The purpose of code blocks this is to define statements to be executed together.
One place you will find statements grouped together in blocks, are in JavaScript functions:

Example



Code:
function myFunction() {
    document.getElementById("demo").innerHTML = "Hello Dolly.";
    document.getElementById("myDIV").innerHTML = "How are you?";
}
[size]



[url=tryit5a8d.html?filename=tryjs_statements_blocks]Try it Yourself »[/url][/size]

[th][/th]
In this tutorial we use 4 spaces of indentation for code blocks.
You will learn more about functions later in this tutorial.



JavaScript Keywords

JavaScript statements often start with a keyword to identify the JavaScript action to be performed.
Here is a list of some of the keywords you will learn about in this tutorial:
[th]Keyword[/th][th]Description[/th]
breakTerminates a switch or a loop
continueJumps out of a loop and starts at the top
debuggerStops the execution of JavaScript, and calls (if available) the debugging function
do ... whileExecutes a block of statements, and repeats the block, while a condition is true
forMarks a block of statements to be executed, as long as a condition is true
functionDeclares a function
if ... elseMarks a block of statements to be executed, depending on a condition
returnExits a function
switchMarks a block of statements to be executed, depending on different cases
try ... catchImplements error handling to a block of statements
varDeclares a variable

[th][/th]
JavaScript keywords are reserved words. Reserved words cannot be used as names for variables.

COMMENTS



JavaScript comments can be used to explain JavaScript code, and to make it more readable.
JavaScript comments can also be used to prevent execution, when testing alternative code.


Single Line Comments

Single line comments start with //.
Any text between // and the end of a line, will be ignored by JavaScript (will not be executed).
This example uses a single line comment before each line, to explain the code:

Example



Code:
// Change heading:
document.getElementById("myH").innerHTML = "My First Page";
// Change paragraph:
document.getElementById("myP").innerHTML = "My first paragraph.";





This example uses a single line comment at the end of each line, to explain the code:

Example


Code:
var x = 5;      // Declare x, give it the value of 5
var y = x + 2;  // Declare y, give it the value of x + 2






Multi-line Comments

Multi-line comments start with /* and end with */.
Any text between /* and */ will be ignored by JavaScript.
This example uses a multi-line comment (a comment block) to explain the code:

Example

/*
The code below will change
the heading with id = "myH"
and the paragraph with id = "myP"
in my web page:


Code:
*/
document.getElementById("myH").innerHTML = "My First Page";
document.getElementById("myP").innerHTML = "My first paragraph.";





[th][/th]
It is most common to use single line comments.
Block comments are often used for formal documentation.



Using Comments to Prevent Execution

Using comments to prevent execution of code, are suitable for code testing.
Adding // in front of a code line changes the code lines from an executable line to a comment.
This example uses // to prevent execution of one of the code lines:

Example



Code:
//document.getElementById("myH").innerHTML = "My First Page";
document.getElementById("myP").innerHTML = "My first paragraph.";




This example uses a comment block to prevent execution of multiple lines:

Example



Code:
/*
document.getElementById("myH").innerHTML = "My First Page";
document.getElementById("myP").innerHTML = "My first paragraph.";
*/
avatar
Admin
Admin
Admin

Posts : 83
Points : 7847
Reputation : 3
Join date : 2015-07-22

View user profile http://droid.1talk.net

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum