JavaScript Statements
In JavaScript, statements are individual instructions that are executed sequentially to perform specific actions or operations. Here's an overview of some common types of JavaScript statements:
Example
let a, b, c; // Statement 1 a = 4; // Statement 2 b = 5; // Statement 3 c = a + b; // Statement 4
Variable Declaration:
Example
var x; // Declaration var y = 5; // Declaration with initialization
Assignment:
Example
x = 10; // Assignment
Conditional Statements: if...else Statement:
Example
if (condition) { // code to be executed if condition is true } else { // code to be executed if condition is false }
switch Statement:
Example
switch (expression) { case value1: // code to be executed if expression === value1 break; case value2: // code to be executed if expression === value2 break; default: // code to be executed if expression doesn't match any case }
Loops:for Loop:
Example
for (initialization; condition; increment/decrement) { // code to be executed repeatedly }
while Loop:
Example
while (condition) { // code to be executed as long as condition is true }
do...while Loop:
Example
do { // code to be executed at least once, then repeatedly if condition is true } while (condition);
Function Declaration:
Example
function functionName(parameter1, parameter2) { // code to be executed }
Return Statement::
Example
return value; // Exits the function and returns a value
Break and Continue:
- break Statement: Used to exit a loop or switch statement.
- continue Statement: Used to skip the current iteration of a loop and continue with the next iteration.
Throw Statement:
Example
throw 'Error message'; // Throws an error