Expressions and Statements.
In JavaScript, statements are the building blocks of a program. They are instructions that perform actions. Here is a list of commonly used statements in JavaScript:
1. Declaration Statements
-
Variable Declaration:
let x = 10;
const y = 20;
var z = 30; -
Function Declaration:
function greet() {
console.log("Hello, World!");
}
2. Control Flow Statements
- Conditional Statements:
-
if...else:
if (condition) {
// code block to be executed if condition is true
} else {
// code block to be executed if condition is false
} -
else if:
if (condition1) {
// code block to be executed if condition1 is true
} else if (condition2) {
// code block to be executed if condition2 is true
} else {
// code block to be executed if both condition1 and condition2 are false
} -
switch:
switch (expression) {
case value1:
// code block to be executed if expression === value1
break;
case value2:
// code block to be executed if expression === value2
break;
default:
// code block to be executed if none of the cases match
}
-
3. Looping Statements
-
for:
for (let i = 0; i < 5; i++) {
console.log(i);
} -
while:
let i = 0;
while (i < 5) {
console.log(i);
i++;
} -
do...while:
let i = 0;
do {
console.log(i);
i++;
} while (i < 5); -
for...in:
const person = { fname: "John", lname: "Doe", age: 25 };
for (let key in person) {
console.log(key + ": " + person[key]);
} -
for...of:
const array = [1, 2, 3, 4, 5];
for (let value of array) {
console.log(value);
}
4. Exception Handling Statements
-
try...catch:
try {
// code block to try
} catch (error) {
// code block to handle errors
} finally {
// code block to be executed regardless of try / catch result
} -
throw:
throw new Error("Something went wrong");
5. Control Statements
-
break:
for (let i = 0; i < 10; i++) {
if (i === 5) break;
console.log(i);
} -
continue:
for (let i = 0; i < 10; i++) {
if (i === 5) continue;
console.log(i);
}
6. Other Statements
-
return:
function add(a, b) {
return a + b;
} -
with (Note:
with
is generally discouraged due to its negative impact on readability and maintainability):const obj = { a: 1, b: 2, c: 3 };
with (obj) {
console.log(a, b, c);
} -
debugger:
debugger; // Stops the execution of JavaScript, and calls the debugging function
-
label:
labelName: {
// code block
break labelName;
}11. Labeled Statements
-
label: Provides a statement with an identifier that you can refer to using break or continue.
outerLoop: for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (i === 1 && j === 1) {
break outerLoop;
}
console.log(`i = ${i}, j = ${j}`);
}
}
7. Import and Export Statements (ES6 Modules)
-
import:
import { myFunction } from './myModule.js';
-
export:
export const myFunction = () => {
// function code
};
References
In JavaScript (and most programming languages), understanding the difference between expressions and statements is fundamental. Here’s a detailed explanation of each and their differences:
Expressions
An expression is any valid unit of code that resolves to a value. Expressions can be as simple as a single value or variable, or they can be complex and involve operators and function calls. Essentially, expressions produce values.
Examples of Expressions:
-
Literal Values:
42 // A number literal
"Hello" // A string literal
true // A boolean literal -
Variable and Constant References:
let x = 10;
x // Resolves to the value of x, which is 10 -
Operations:
5 + 3 // Arithmetic expression that evaluates to 8
a * b // Multiplicative expression that evaluates to the product of a and b -
Function Calls:
Math.max(10, 20) // Function call expression that evaluates to 20
-
Object and Array Literals:
{ name: "Alice" } // Object literal expression
[1, 2, 3] // Array literal expression -
Logical Expressions:
x && y // Logical AND expression
Statements
A statement is a unit of code that performs an action. Statements are like sentences in natural language: they perform actions but do not necessarily produce values directly. Statements often contain expressions, and they define the structure and flow of a program.
Examples of Statements:
-
Variable Declarations:
let x = 10; // Variable declaration statement
-
Control Flow Statements:
if (x > 5) { // If statement
console.log("x is greater than 5");
}
for (let i = 0; i < 10; i++) { // For loop statement
console.log(i);
} -
Function Declarations:
function greet() { // Function declaration statement
console.log("Hello, world!");
} -
Return Statements:
return x; // Return statement
Key Differences
-
Purpose:
- Expression: Produces a value.
- Statement: Performs an action.
-
Usage:
- Expression: Can be used wherever a value is expected (e.g., inside another expression, as function arguments).
- Statement: Cannot be used where a value is expected; it forms the structure and flow of the code.
-
Execution Context:
- Expression: Resolves to a value and can be part of a larger expression.
- Statement: Executes an action and usually ends with a semicolon.
-
In JSX:
- Expressions: Can be embedded in JSX.
- Statements: Cannot be embedded directly in JSX.
Examples Combining Both
let x = 10; // Variable declaration statement
let y = x + 5; // Expression within a variable declaration statement
function isPositive(n) { // Function declaration statement
if (n > 0) { // If statement
return true; // Return statement within if statement
} else {
return false; // Return statement within else clause
}
}
console.log(isPositive(15)); // Expression (function call) within a statement (console.log)
In Summary
- Expressions are pieces of code that resolve to a value and can be used within other expressions.
- Statements are pieces of code that perform actions and define the flow and structure of the program.
Understanding this distinction is crucial for writing and debugging code effectively, especially in a language like JavaScript where both expressions and statements are heavily used.