JavaScript Statements
In HTML, JavaScript statements are 'executed' instructions by web browsers.
Example
var x, y, z; // Statement 1 x = 22; // Statement 2 y = 11; // Statement 3 z = x + y; // Statement 4
JavaScript program
Computer programare a series of 'instructions' executed by a computer.
In programming languages, these programmingInstructionis called a statement.
JavaScript programis a series of programmingStatement.
Note:In HTML, JavaScript programs are executed by web browsers.
JavaScript Statements
JavaScript statements are composed of the following:
Values, operators, expressions, keywords, and comments.
This statement tells the browser to output "Hello Kitty." in the HTML element with id="demo":
Example
document.getElementById("demo").innerHTML = "Hello Kitty.";
Most JavaScript programs contain many JavaScript statements.
These statements will be executed one by one in the order they are written.
Note:JavaScript programs (and JavaScript statements) are often referred to as JavaScript code.
Semicolon ;
Semicolons separate JavaScript statements.
Please add a semicolon after each executable statement:
a = 5; b = 6; c = a + b;
If separated by semicolons, multiple statements can be written on the same line:
a = 5; b = 6; c = a + b;
You may have seen examples online without semicolons.
Hint:Ending statements with a semicolon is not necessary, but we still strongly recommend it.
JavaScript whitespace characters
JavaScript ignores multiple spaces. You can add spaces to the script to enhance readability.
The following two lines are equal:
var person = "Bill"; var person="Bill";
It is a good habit to add spaces next to operators ( = + - * / ):
var x = y + z;
JavaScript line length and line breaks
To achieve the best readability, programmers often prefer to keep code lines within 80 characters.
If a JavaScript statement is too long, the best place to break it is at an operator:
Example
document.getElementById("demo").innerHTML = "Hello Kitty.";
JavaScript code block
JavaScript statements can be enclosed in curly braces ({
...}
)are combined within the code block.
The purpose of a code block is to define statements that are executed together.
You will see statements grouped together in blocks in JavaScript:
Example
function myFunction() { document.getElementById("demo").innerHTML = "Hello Kitty."; document.getElementById("myDIV").innerHTML = "How are you?"; }
Note:In this tutorial, we use 4 spaces of indentation for code blocks.
Hint:You will learn more about functions later in this tutorial.
JavaScript Keywords
JavaScript statements often use a keyword to identify the JavaScript action to be executed.
The following table lists some of the keywords that will be learned in this tutorial:
Keyword | Description |
---|---|
break | Terminate a switch or loop. |
continue | Break out of the loop and start at the top. |
debugger | Stop executing JavaScript and call the debugging function (if available). |
do ... while | Execute statement blocks and repeat the code block when the condition is true. |
for | Mark statement blocks to be executed as long as the condition is true. |
function | Declare functions. |
if ... else | Mark statement blocks to be executed, depending on a certain condition. |
return | Exit the function. |
switch | Mark statement blocks to be executed, depending on different situations. |
try ... catch | Implement error handling for statement blocks. |
var | Declare variables. |
Note:JavaScript keywords refer to reserved words. Reserved words cannot be used as variable names.
Supplementary Reading
Advanced JavaScript Tutorial: ECMAScript Syntax