Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

Updated
4 min read

When writing programs in JavaScript, we often perform operations such as:

  • Adding numbers

  • Comparing values

  • Checking conditions

To perform these actions, JavaScript provides operators.

Operators help us manipulate values and perform calculations in code.


What Are Operators?

An operator is a symbol used to perform an operation on values.

Example:

let result = 5 + 3;

Here:

  • 5 and 3 are operands

  • + is the operator

Output:

8

Operators allow JavaScript to perform mathematical operations, comparisons, and logical checks.


Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

Operator Meaning Example
+ Addition 5 + 3
- Subtraction 5 - 3
* Multiplication 5 * 3
/ Division 6 / 3
% Modulus (remainder) 7 % 2

Example:

let a = 10;
let b = 5;

console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);
console.log(a % b);

Output:

15
5
50
2
0

The % operator returns the remainder after division.

Example:

7 % 2 = 1

Comparison Operators

Comparison operators compare two values and return true or false.

Operator Meaning
== Equal to
=== Strict equal
!= Not equal
> Greater than
< Less than

Example:

console.log(5 > 3);
console.log(5 < 3);
console.log(5 == "5");
console.log(5 === "5");

Output:

true
false
true
false

Difference Between == and ===

This is a very important concept.

== (Loose Equality)

Compares values only.

console.log(5 == "5");

Output:

true

JavaScript converts the string "5" into a number.


=== (Strict Equality)

Compares value AND data type.

console.log(5 === "5");

Output:

false

Because:

  • 5 → number

  • "5" → string

Best practice: prefer === in most cases.


Logical Operators

Logical operators are used when working with conditions.

Operator Meaning
&& AND
`
! NOT

Logical AND &&

Returns true only if both conditions are true.

let age = 20;

console.log(age > 18 && age < 30);

Output:

true

Logical OR ||

Returns true if at least one condition is true.

console.log(5 > 10 || 10 > 5);

Output:

true

Logical NOT !

Reverses a boolean value.

console.log(!true);

Output:

false

Truth Table for Logical Operators

| A | B | A && B | A || B | | --- | --- | --- | --- | | true | true | true | true | | true | false | false | true | | false | true | false | true | | false | false | false | false |


Assignment Operators

Assignment operators assign values to variables.

Operator Example Meaning
= x = 5 Assign value
+= x += 2 Add and assign
-= x -= 2 Subtract and assign

Example:

let x = 10;

x += 5;
console.log(x);

x -= 3;
console.log(x);

Output:

15
12

Assignment Practice

Perform Arithmetic Operations

let a = 8;
let b = 4;

console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);

Compare Values Using == and ===

console.log(10 == "10");
console.log(10 === "10");

Output:

true
false

Logical Operator Example

let age = 22;

if (age > 18 && age < 60) {
  console.log("Eligible to work");
}

Output:

Eligible to work

Operator Categories Summary

Category Operators
Arithmetic + - * / %
Comparison == === != > <
Logical `&&
Assignment = += -=