JavaScript Statements
JavaScript statements are commands to the browser. The purpose of these commands is to tell the browser what to do.
JavaScript Statements
JavaScript statements are commands given to the browser.
These commands tell the browser what actions to take.
The following JavaScript statement outputs the text "Hello Dolly" to the HTML element with id="demo":
Example
document.getElementById("demo").innerHTML = "Hello Dolly";
Semicolon ;
Semicolons are used to separate JavaScript statements.
Typically, we add a semicolon at the end of each executable statement.
Another use of semicolons is to write multiple statements on one line.
a = 5;
b = 6;
c = a + b;
The above example can also be written like this:
a = 5; b = 6; c = a + b;
| | You might also see examples without semicolons. <br>In JavaScript, using a semicolon to end a statement is optional. | | --- | --- |
JavaScript Code
JavaScript code is a sequence of JavaScript statements.
The browser executes each statement in the order they are written.
This example outputs a heading and two paragraphs to the web page:
Example
document.getElementById("demo").innerHTML = "Hello Dolly";
document.getElementById("myDIV").innerHTML = "How are you?";
JavaScript Code Blocks
JavaScript statements can be grouped together in blocks.
Blocks start with a left curly brace {
and end with a right curly brace }
.
The purpose of blocks is to execute a sequence of statements together.
This example outputs a heading and two paragraphs to the web page:
Example
function myFunction() {
document.getElementById("demo").innerHTML = "Hello Dolly";
document.getElementById("myDIV").innerHTML = "How are you?";
}
You will learn more about functions in later chapters.
JavaScript Statement Identifiers
JavaScript statements often start with a statement identifier and execute the statement.
Statement identifiers are reserved keywords and cannot be used as variable names.
The following table lists JavaScript statement identifiers (keywords):
Statement | Description |
---|---|
break | Exits a loop. |
catch | Block of statements that executes if an error occurs in the try block. |
continue | Skips one iteration in a loop. |
do ... while | Executes a block of statements and repeats the block while a condition is true. |
for | Executes a block of code a specified number of times while a condition is true. |
for ... in | Iterates over the properties of an object or elements of an array. |
function | Defines a function. |
if ... else | Executes different actions based on different conditions. |
return | Exits a function. |
switch | Executes different actions based on different conditions. |
throw | Throws (generates) an error. |
try | Implements error handling, used with catch. |
var | Declares a variable. |
while | Executes a block of statements while a condition is true. |
Whitespace
JavaScript ignores extra whitespace. You can add whitespace to your script to improve readability. The following two lines of code are equivalent:
var person = "tutorialpro";
var person = "tutorialpro";
Line Breaks in Code
You can break a code line within a text string using a backslash. The following example will display correctly:
Example
document.write("Hello \
World!");
However, you cannot break a line like this:
document.write \
("Hello World!");
Key Point: JavaScript is an interpreted language, meaning the browser executes the script line by line as it reads the code. In contrast, traditional programming languages compile all the code before execution.