Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

Updated
4 min read
JavaScript Operators: The Basics You Need to Know

When writing JavaScript code, you often need to perform calculations, compare values, or combine conditions. To do these tasks, JavaScript uses something called operators.

If you’ve ever done basic math like 5 + 3, you’ve already used an operator. In programming, operators help you perform actions on values and variables.

In this article, we’ll explore some of the most common JavaScript operators that every beginner should understand.

What Are Operators?

An operator is a symbol that performs an operation on values.

For example:

let result = 10 + 5;
console.log(result);

Output:

15

In this example:

  • 10 and 5 are values (operands)

  • + is the operator

  • The operator performs the addition

JavaScript provides different types of operators depending on the task you want to perform.

Some of the most commonly used ones include:

  • Arithmetic operators

  • Comparison operators

  • Logical operators

  • Assignment operators

Let’s look at each of them with simple examples.

Arithmetic Operators

Arithmetic operators are used for basic mathematical calculations, just like in regular math.

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

Here are the most common arithmetic operators:

Operator Description Example
+ Addition 5 + 3
- Subtraction 5 - 2
* Multiplication 5 * 2
/ Division 10 / 2
% Modulus (remainder) 10 % 3

The modulus operator % returns the remainder after division.

Example:

console.log(10 % 3);

Output:

1

This means 10 divided by 3 leaves a remainder of 1.

Comparison Operators

Comparison operators are used to compare two values.

They return either:

  • true

  • false

Example:

let a = 10;
let b = 5;

console.log(a > b);
console.log(a < b);

Output:

true
false

Here are some common comparison operators:

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

The Difference Between == and ===

This is one of the most important things beginners should understand.

Using ==

== checks if two values are equal after type conversion.

Example:

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

Output:

true

JavaScript converts "5" (a string) into a number before comparing it.

Using ===

=== checks both value and data type.

Example:

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

Output:

false

Because:

  • 5 is a number

  • "5" is a string

For this reason, developers usually prefer using === to avoid unexpected results.

Logical Operators

Logical operators are used when you want to combine multiple conditions.

These operators are commonly used in conditional statements like if.

Example:

let age = 20;
let hasLicense = true;

console.log(age >= 18 && hasLicense);

Output:

true

Common logical operators include:

Operator Meaning
&& AND
`
! NOT

AND (&&)

Returns true only if both conditions are true.

Example:

console.log(true && true);

Output:

true

OR (||)

Returns true if at least one condition is true.

Example:

console.log(true || false);

Output:

true

NOT (!)

The NOT operator reverses a boolean value.

Example:

console.log(!true);

Output:

false

Assignment Operators

Assignment operators are used to assign values to variables.

The most common one is the = operator.

Example:

let score = 50;

Here, the value 50 is assigned to the variable score.

JavaScript also provides shorthand assignment operators that combine operations.

Example:

let score = 10;

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

Output:

15

Common assignment operators include:

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

Another example:

let points = 20;

points -= 5;

console.log(points);

Output:

15