Statement vs Expression in JavaScript
Understanding the difference between statements and expressions is essential to grasp the flow and logic of JavaScript code. 1. Expression An expression produces a value. It can be used wherever a value is expected. Examples : // Arithmetic expression 2 + 2 // String expression "Hello" + " World" // Function call expression Math.max(5, 10) // Boolean expression 5 > 3 Expressions can be part of a larger expression: let sum = (2 + 3) * 5; // Expression `2 + 3` is evaluated to 5 2. Statement A statement performs an action. It is a complete unit of execution but does not return a value directly. Examples : // Variable declaration (statement) let x; // Conditional statement if (x > 0) { console.log("x is positive"); } // Loop statement for (let i = 0; i < 5; i++) { console.log(i); } Statements typically control the flow of the program (e.g., loops, conditionals) and do not produce values directly. Key Differences Aspect...