In JavaScript, statements are individual instructions or commands that make up a program. Each statement performs a specific action or operation. Here are some common types of JavaScript statements:
1. Variable Declaration:
- Used to declare variables and allocate memory for storing data.
- Example: `let message = "Hello, world!";`
2. Assignment:
- Used to assign values to variables or update their values.
- Example: `message = "Welcome, user!";`
3. Conditional Statements:
- Used to make decisions based on conditions and execute different blocks of code.
- Example:
javascript
if (score > 90) {
console.log("Excellent!");
} else {
console.log("Keep up the good work!");
}
4. Loops:
- Used to repeat a block of code multiple times until a condition is met.
- Examples:
- `for` loop:
javascript
for (let i = 0; i < 5; i++) {
console.log(i);
}
- `while` loop:
javascript
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
5. Function Declaration:
- Used to define reusable blocks of code that can be executed later.
- Example:
javascript
function greet(name) {
console.log("Hello, " + name + "!");
}
6. Return Statement:
- Used to specify the value to be returned by a function.
- Example:
javascript
function add(a, b) {
return a + b;
}
7. Error Handling:
- Statements used to handle errors or exceptions in code execution.
- Example:
javascript
try {
// Code that may throw an error
} catch (error) {
// Code to handle the error
}
8. Control Flow Statements:
- Statements used to control the flow of program execution, such as `break`, `continue`, and `switch`.
These are some of the common types of statements in JavaScript. JavaScript supports a wide range of statements that enable you to control program flow, perform operations, and interact with data. By combining these statements, you can create complex and dynamic programs in JavaScript.
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc