Facebook Pixel

Operators and Expressions

When writing code in JavaScript, you’ll often need to perform operations — whether it’s adding numbers, comparing values, or combining logic. That’s where operators come in.

An operator is simply a symbol that tells JavaScript to perform a specific action on one or more operands (which can be values, variables, or expressions).

For example:

let sum = 10 + 5; // '+' is the addition operator

Here, the + operator adds two numbers together.

Types of Operators in JavaScript

JavaScript provides many types of operators to perform different kinds of tasks. Let’s go through the main ones

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

Operator Description Example Result
+ Addition 5 + 2 7
- Subtraction 5 - 2 3
* Multiplication 5 * 2 10
/ Division 10 / 2 5
% Modulus (Remainder) 10 % 3 1
** Exponentiation 2 ** 3 8

Example:

let a = 10;
let b = 3;
console.log(a % b); // 1

2. Comparison Operators

Comparison operators compare two values and return a boolean (true or false).

Operator Description Example Result
== Equal to (checks value only) 5 == "5" true
=== Strict equal (checks value & type) 5 === "5" false
!= Not equal to (checks value only) 5 != "6" true
!== Strict not equal 5 !== "5" true
> Greater than 10 > 5 true
< Less than 2 < 5 true
>= Greater than or equal to 5 >= 5 true
<= Less than or equal to 4 <= 3 false

Example:

let age = 18;
console.log(age >= 18); // true

3. Logical Operators

Logical operators are used to combine multiple conditions.

Operator Description Example Result
&& Logical AND (true if both are true) true && false false
` ` Logical OR (true if one is true) `true false` true
! Logical NOT (reverses boolean value) !true false

Example:

let age = 20;
let hasLicense = true;
 
if (age >= 18 && hasLicense) {
  console.log("You can drive!");
}

4. Assignment Operators

Assignment operators are used to assign values to variables. The most common one is =, but there are shorthand forms for performing operations and assigning at the same time.

Operator Description Example Equivalent To
= Assigns a value x = 5
+= Adds and assigns x += 5 x = x + 5
-= Subtracts and assigns x -= 5 x = x - 5
*= Multiplies and assigns x *= 5 x = x * 5
/= Divides and assigns x /= 5 x = x / 5
%= Modulus and assigns x %= 2 x = x % 2

Example:

let num = 10;
num += 5; // same as num = num + 5
console.log(num); // 15

5. Conditional (Ternary) Operator

The ternary operator is a shorthand way to write an if-else statement.

Syntax:

condition ? valueIfTrue : valueIfFalse

Example:

let age = 17;
let message = age >= 18 ? "You are an adult" : "You are a minor";
console.log(message); // "You are a minor"

What Are Expressions?

An expression is any valid combination of variables, values, and operators that produces a result.

For example:

let x = 10;
let y = 20;
let z = x + y; // Expression → x + y = 30

Here, x + y is an expression that evaluates to 30, which is then assigned to z.

Operator Precedence (Order of Operations)

When an expression has multiple operators, JavaScript follows a specific order of execution known as operator precedence.

Example:

let result = 10 + 5 * 3;
console.log(result); // 25

Why 25? Because multiplication (*) has higher precedence than addition (+), so 5 * 3 runs first.

If you want to change the order, use parentheses:

let result = (10 + 5) * 3;
console.log(result); // 45