Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

Updated
6 min read

When writing JavaScript, you constantly perform actions on data. Sometimes you add numbers, sometimes you compare values, and sometimes you check conditions before running code.

This is where operators come in.

Operators are small symbols that tell JavaScript what action should be performed on values. They allow us to perform calculations, compare data, combine conditions, and update variables.

If you have ever written something like:

let result = 5 + 3;

You have already used an operator.

In this article, we will explore the most common JavaScript operators that beginners need to know.

What Are Operators in JavaScript?

In simple terms, operators are symbols used to perform operations on values or variables.

For example:

let a = 10;
let b = 5;

console.log(a + b);

Here:

  • a and b are operands (the values)

  • + is the operator

  • The operator tells JavaScript to add the values

Operators act like actions between values.

Just like in mathematics:

5 + 3

The + symbol tells us to add the numbers.

JavaScript works in the same way, but it includes many types of operators for different purposes.

The main operator categories we will cover are:

Category Purpose
Arithmetic Perform calculations
Comparison Compare two values
Logical Combine conditions
Assignment Assign or update variables

Arithmetic Operators

Arithmetic operators perform basic mathematical calculations.

These are the operators you already know from math.

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

Example

let num1 = 10;
let num2 = 3;

console.log(num1 + num2); // 13
console.log(num1 - num2); // 7
console.log(num1 * num2); // 30
console.log(num1 / num2); // 3.33...
console.log(num1 % num2); // 1

The modulus operator (%) returns the remainder of a division.

Example:

10 % 3 = 1

Because:

3 × 3 = 9
remainder = 1

This operator is often used for things like checking even and odd numbers.

Comparison Operators

Comparison operators compare two values and return a boolean result.

A boolean value is either:

true
false

These operators are commonly used in conditions and decision-making.

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

Example

let a = 10;
let b = 5;

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

Output:

true
false
false

The Difference Between == and ===

This is one of the most important concepts for beginners.

== (Loose Equality)

== compares values after converting their types if needed.

Example:

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

Output:

true

Because JavaScript converts "5" into the number 5.

=== (Strict Equality)

=== compares both value and type.

Example:

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

Output:

false

Because:

  • 5 is a number

  • "5" is a string

Even though the values look the same, the types are different.

Best Practice

Most developers prefer using === because it avoids unexpected type conversions.

Logical Operators

Logical operators allow us to combine or modify conditions.

They are commonly used in if statements and decision-making.

Operator Meaning
&& AND
`
! NOT

Logical AND (&&)

The AND operator returns true only if both conditions are true.

Example:

let age = 20;
let hasID = true;

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

Output:

true

Both conditions are true.

Logical OR (||)

The OR operator returns true if at least one condition is true.

Example:

let isWeekend = true;
let isHoliday = false;

console.log(isWeekend || isHoliday);

Output:

true

Because one condition is true.

Logical NOT (!)

The NOT operator reverses a boolean value.

Example:

let isLoggedIn = true;

console.log(!isLoggedIn);

Output:

false

Logical Operators Truth Table

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

This table shows how logical operators behave with different boolean values.

Assignment Operators

Assignment operators are used to store or update values in variables.

The most common assignment operator is:

=

Example:

let score = 10;

This assigns the value 10 to the variable score.

Compound Assignment Operators

JavaScript also provides shortcuts to update values.

Operator Example Meaning
+= x += 5 x = x + 5
-= x -= 2 x = x - 2

Example

let points = 10;

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

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

Output:

15
12

These operators make code shorter and cleaner.

Small Practice Example

Let's combine what we've learned.

let num1 = 10;
let num2 = 5;

// arithmetic
let sum = num1 + num2;
console.log("Sum:", sum);

// comparison
console.log(num1 == "10");
console.log(num1 === "10");

// logical
let age = 20;
let hasTicket = true;

if (age >= 18 && hasTicket) {
  console.log("Entry allowed");
}

This example demonstrates:

  • Arithmetic calculation

  • Comparison using == and ===

  • Logical condition with &&

Conclusion

Operators are one of the fundamental building blocks of JavaScript. They allow us to perform calculations, compare values, evaluate conditions, and update variables.

In this article we explored:

  • Arithmetic operators for calculations

  • Comparison operators for evaluating values

  • Logical operators for combining conditions

  • Assignment operators for storing and updating variables

Once you start building real applications, you will use these operators constantly.

The best way to understand them is to experiment in the browser console and try different combinations yourself.

With these basics in place, you're ready to write more powerful JavaScript logic.