JavaScript Statements Tutorial
JavaScript statements are the commands that tell the browser what action to perform. They are the building blocks of JavaScript code, and they can be used to perform a wide variety of tasks, such as:
Declaring and initializing variables
Performing mathematical and logical operations
Controlling the flow of execution with loops and conditional statements
Calling functions
Interacting with the DOM
JavaScript statements are separated by semicolons (;). For example, the following code declares two variables and initializes them to the values 1 and 2, respectively:
let a = 1;
let b = 2;
The following code performs a mathematical operation on the two variables and assigns the result to a new variable:
let c = a + b;
The following code uses a conditional statement to check the value of the variable c
and print a different message to the console depending on the result:
if (c > 2) {
console.log("c is greater than 2.");
} else {
console.log("c is less than or equal to 2.");
}
The following code calls a function named myFunction()
:
myFunction();
The following code uses the DOM to change the text of an element with the ID myElement
:
document.getElementById("myElement").textContent = "Hello, world!";
Types of JavaScript Statements
There are many different types of JavaScript statements, but some of the most common ones include:
Declaration statements: These statements are used to declare variables and functions.
Assignment statements: These statements are used to assign values to variables.
Expression statements: These statements evaluate an expression and return a value.
Conditional statements: These statements control the flow of execution based on the evaluation of a condition.
Loop statements: These statements repeat a block of code until a condition is met.
Function statements: These statements define functions.
Code Blocks
JavaScript statements can be grouped together inside curly brackets { }
. This is called a code block. Code blocks are useful for organizing your code and making it more readable. For example, the following code uses a code block to group together the statements that are executed when the myFunction()
function is called:
function myFunction() {
// Code block
console.log("Hello from myFunction()!");
}
JavaScript Keywords
JavaScript keywords are special words that have a special meaning to the JavaScript interpreter. They cannot be used as variable names or for any other purpose. Some common JavaScript keywords include:
var
let
const
function
if
else
else if
while
do
for
switch
case
break
continue
return
Examples of JavaScript Statements
Here are some examples of common JavaScript statements:
// Declaration statementslet myVariable;
function myFunction() {}
// Assignment statements
myVariable = 1;
myFunction(2);
// Expression statements
console.log(myVariable + 2);
// Conditional statements
if (myVariable > 2)
{
console.log("myVariable is greater than 2.");
}
else {
console.log("myVariable is less than or equal to 2.");
}
// Loop statements
while (myVariable < 5)
{
console.log(myVariable);
myVariable++;
}
// Function statements
function myFunction(x)
{
return x + 1;
}
Conclusion
JavaScript statements are the building blocks of JavaScript code. They can be used to perform a wide variety of tasks, such as declaring and initializing variables, performing mathematical and logical operations, controlling the flow of execution with loops and conditional statements, calling functions, and interacting with the DOM.