What is a JavaScript Expression?

In programming, an expression is a combination of variables, values, operators, and functions that produces a single value. It can be as simple as a single variable or as complex as a combination of multiple operations.


Types of JavaScript Expressions

Literal Expressions: These are constants or values directly written in the code, such as numbers, strings, arrays, and objects.


Example:


let number = 10; // Number literal expression
let string = 'Hello'; // String literal expression
let array = [1, 2, 3]; // Array literal expression
let object = { key: 'value' }; // Object literal expression
 

 

Arithmetic Expressions: These involve arithmetic operations like addition, subtraction, multiplication, and division.


Example:
 

let result = 5 + 3 * 2; // Arithmetic expression

 

Logical Expressions: These involve logical operations such as AND, OR, and NOT.

 

Example:
 

let isTrue = (true && false) || (true && true); // Logical expression

 

String Expressions: These involve string operations like concatenation.


Example:
 

let fullName = 'John' + ' ' + 'Doe'; // String expression

 

Comparison Expressions: These involve comparison operations like equal to, not equal to, greater than, etc.


Example:
 

let isEqual = (5 == '5'); // Comparison expression

 

Conditional (Ternary) Expressions: These are expressions that involve the conditional (ternary) operator ? :.


Example:
 

let result = (age >= 18) ? 'Adult' : 'Minor'; // Conditional expression

 

Function Expressions: These involve defining functions and calling them.


Example:
 

let square = function(x) { return x * x; }; // Function expression
let squaredValue = square(5); // Calling the function expression

 

Array and Object Initializer Expressions: These are expressions used to initialize arrays and objects.


Example:
 

let numbers = [1, 2, 3, 4]; // Array initializer expression
let person = { name: 'Alice', age: 30 }; // Object initializer expression

 

Examples of JavaScript Expressions

Let's dive deeper into each type of expression with examples:


Literal Expressions:
 


let number = 10; // Number literal expression
let string = 'Hello'; // String literal expression
let array = [1, 2, 3]; // Array literal expression
let object = { key: 'value' }; // Object literal expression
 



Arithmetic Expressions:
 

let result = 5 + 3 * 2; // Arithmetic expression
let total = (10 + 5) * 2; // Parentheses can change the order of operations



Logical Expressions:
 

let isTrue = (true && false) || (true && true); // Logical expression
let isFalse = !true; // Logical NOT expression



String Expressions:
 

let fullName = 'John' + ' ' + 'Doe'; // String expression
let greeting = 'Hello, ' + fullName + '!'; // Concatenation with variables



Comparison Expressions:
 

let isEqual = (5 == '5'); // Comparison expression
let isGreater = (10 > 5); // Greater than comparison expression


Conditional (Ternary) Expressions:
 

let age = 20;
let isAdult = (age >= 18) ? 'Adult' : 'Minor'; // Conditional expression



Function Expressions:
 

let square = function(x) { return x * x; }; // Function expression
let squaredValue = square(5); // Calling the function expression



Array and Object Initializer Expressions:
 

let numbers = [1, 2, 3, 4]; // Array initializer expression
let person = { name: 'Alice', age: 30 }; // Object initializer expression



Evaluating JavaScript Expressions

Expressions are often used in assignments, function calls, conditionals, and loops to perform computations and make decisions. For example:


Assigning the result of an expression to a variable:
 

let result = 5 + 3; // Evaluates to 8



Using expressions in function calls:
 

console.log(10 * 2); // Evaluates to 20


Using expressions in conditionals:
 

let age = 25;
if (age >= 18) {
    console.log('You are an adult.');
} else {
    console.log('You are a minor.');
}



Using expressions in loops:
 

for (let i = 0; i < 5; i++) {
    console.log(i); // Outputs numbers from 0 to 4
}

 



Practice Excercise Practice now