A JavaScript statement is a command or instruction that is executed by a web browser or a JavaScript engine. JavaScript statements are used to perform actions, manipulate data, and control the flow of a program.
Here are some common types of JavaScript statements:
1. *Variable declarations*: `let`, `const`, `var`
2. *Assignment statements*: `x = 5`, `y = "hello"`
3. *Conditional statements*: `if (x > 5) { ... }`, `if (y === "hello") { ... }`
4. *Looping statements*: `for (let i = 0; i < 5; i++) { ... }`, `while (x < 5) { ... }`
5. *Function declarations*: `function add(x, y) { return x + y; }`
6. *Function calls*: `add(2, 3)`, `console.log("Hello")`
7. *Return statements*: `return x;`, `return "Hello";`
8. *Alert statements*: `alert("Hello");`
9. *Debugging statements*: `console.log("Hello");`, `debugger;`
JavaScript statements are typically separated by semicolons (`;`) and are executed in the order they are written.
Here is an example of a simple JavaScript program that uses several statements:
```
let x = 5;
if (x > 10) {
console.log("x is greater than 10");
} else {
console.log("x is less than or equal to 10");
}
```
This program declares a variable `x`, checks if it is greater than 10, and logs a message to the console depending on the result.
0 टिप्पणियाँ