Table of Contents
show
In JavaScript expressions and statements are two fundamental building blocks used to construct programs. They serve different purposes and have different characteristics.
Expressions
- An expression is any valid unit of code that evaluates to a value
- It can be as simple as a single value or as complex as a combination of values, variables and operators
- Expressions are often used to compute values or represent relationship between values
Example
“dev” //”abc”
20 // 20
Var1 + Var2 // sum of Var1 and Var2
“Good” + “day” //Good day
X <= Y && !Z // true or false
myFun(a,b) // function result
** Expressions can be used as arguments in function call.
Statements
- A statement is a complete unit of code that performs an action or operation.
- Statements are executable and may produce side effects, change the program’s state or control the flow of execution.
- A JavaScript program is essentially a sequence of statements executed one after another. The end of the statements are marked with semicolon.
let x = 5; // variable declaration statement
console.log(“Hello, World”); // Function call statement
if(x > 0){ // Conditional Statement
// code block can contain multiple statements and expressions
console.log(x)
}
Good to Know
- Expressions can be turned into statements but vice versa is not possible.
- Return is a statement not an expression
Views: 12