Skip to main content

Command Palette

Search for a command to run...

Understanding Operators in JavaScript

Updated
•5 min read•View as Markdown

When writing JavaScript programs, we often need to perform calculations, compare values, or combine conditions. The symbols that allow us to perform these operations are called operators.

Operators are one of the most basic tools in programming because they allow us to manipulate and evaluate data.

In this article, we will explore:

  • What operators are

  • Arithmetic operators (+, -, *, /, %)

  • Comparison operators (==, ===, !=, >, <)

  • Logical operators (&&, ||, !)

  • Assignment operators (=, +=, -=)

All examples are simple and easy to test in the console.

What Are Operators?

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

Example:

let result = 5 + 3;

Here:

  • 5 and 3 are operands

  • + is the operator

  • The operation produces the result 8

Operators allow programs to perform tasks such as:

  • calculations

  • comparisons

  • decision-making

Categories of JavaScript Operators

JavaScript has several types of operators. In this article we focus on four commonly used ones.

Operator Type Purpose
Arithmetic Perform mathematical calculations
Comparison Compare values
Logical Combine conditions
Assignment Assign values to variables

Arithmetic Operators

Arithmetic operators are used for basic mathematical calculations.

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

Example:

let a = 10;
let b = 3;

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

Output:

13
7
30
3.333...
1

The % operator returns the remainder of a division.

Example:

7 ÷ 2 → remainder = 1

Comparison Operators

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

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

Example:

console.log(5 > 3);
console.log(2 < 1);

Output:

true
false

Difference Between == and ===

This is one of the most important comparisons in JavaScript.

Using ==

== compares values but may convert data types automatically.

Example:

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

Output:

true

Why? Because JavaScript converts "5" to the number 5.

Using ===

=== compares both value and data type.

Example:

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

Output:

false

Because:

  • 5 → number

  • "5" → string

They are not the same type.

In modern JavaScript, developers usually prefer === for safer comparisons.

Logical Operators

Logical operators are used to combine multiple conditions.

Operator Meaning
&& AND
`
! NOT

AND (&&)

Both conditions must be true.

Example:

let age = 20;

if (age > 18 && age < 30) {
  console.log("Young adult");
}

Both conditions must be satisfied.

OR (||)

Only one condition needs to be true.

Example:

let day = "Sunday";

if (day === "Saturday" || day === "Sunday") {
  console.log("Weekend");
}

NOT (!)

The NOT operator reverses a boolean value.

Example:

let isStudent = true;

console.log(!isStudent);

Output:

false

Truth Table for Logical Operators

AND (&&)

A B Result
true true true
true false false
false true false
false false false

OR (||)

A B Result
true true true
true false true
false true true
false false false

Assignment Operators

Assignment operators assign values to variables.

Basic Assignment

let x = 10;

Here = assigns the value 10 to x.

Add and Assign (+=)

Example:

let x = 5;

x += 3;

console.log(x);

Output:

8

This is equivalent to:

x = x + 3

Subtract and Assign (-=)

Example:

let x = 10;

x -= 4;

console.log(x);

Output:

6

Equivalent to:

x = x - 4

Assignment (Practice Exercise)

Try these exercises in your browser console or code editor.

1) 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);
console.log(a % b);

2) Compare Two Values

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

Observe the difference between the results.

3) Write a Condition Using Logical Operators

Example:

let age = 25;
let hasLicense = true;

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

Try changing the values and observe the output.

Final Thoughts

Operators are one of the most fundamental parts of JavaScript. They allow us to perform calculations, compare values, assign data, and create logical conditions in our programs.

By understanding:

  • arithmetic operators for calculations

  • comparison operators for checking values

  • logical operators for combining conditions

  • assignment operators for updating variables

you gain the tools needed to write meaningful program logic.

The best way to learn operators is simple: experiment in the console. Try different values, change operators, and observe how the output changes. Over time, using operators will become second nature in your coding journey.

1 views