There are many different types of JavaScript statements. Some of the most common types of statements include:
- Assignment statements – Assignment statements assign values to variables. For example, the following code assigns the value “Hello, world!” to the variable message:
Code snippet
var message = “Hello, world!”;
- Expression statements – Expression statements evaluate an expression and discard the result. For example, the following code evaluates the expression 1 + 2 and discards the result:
Code snippet
1 + 2;
- Control flow statements – Control flow statements control the flow of execution of a JavaScript program. For example, the following code uses the if statement to execute a block of code if a condition is true:
Code snippet
if (condition) {
// Execute this block of code if the condition is true.
}
- Function statements – Function statements define functions. For example, the following code defines a function called greet():
Code snippet
function greet() {
console.log(“Hello, world!”);
}
- Object statements – Object statements define objects. For example, the following code defines an object called person:
Code snippet
var person = {
name: “John Doe”,
age: 30
};
- Array statements – Array statements define arrays. For example, the following code defines an array called numbers:
Code snippet
var numbers = [1, 2, 3, 4, 5];
These are just a few of the many different types of JavaScript statements. By learning about the different types of statements, you can write JavaScript code that is both efficient and effective.