Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

Published
6 min read
JavaScript Operators: The Basics You Need to Know

Operators in JavaScript are symbols that perform operations on values—such as adding numbers, comparing values, or combining conditions. The most common operator categories you'll use daily are arithmetic, comparison, logical, and assignment operators.


What Are Operators in JavaScript?

In JavaScript, operators are special symbols that perform operations on one or more values (called operands).

For example:

let result = 5 + 3;

Here:

  • 5 and 3 are operands

  • + is the operator

  • The result is 8

Operators allow you to:

  • Perform calculations

  • Compare values

  • Build conditions

  • Assign values to variables

You will use them constantly when writing JavaScript programs.


Categories of Common JavaScript Operators

Category Purpose Example
Arithmetic Operators Perform mathematical calculations 5 + 3
Comparison Operators Compare values and return true or false 5 > 3
Logical Operators Combine or invert conditions a > 5 && b < 10
Assignment Operators Assign or update variable values x += 2

Arithmetic Operators

Arithmetic operators perform basic mathematical calculations.

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

Example: Basic Math Operations

let a = 10;
let b = 3;

console.log(a + b); // 13
console.log(a - b); // 7
console.log(a * b); // 30
console.log(a / b); // 3.333...
console.log(a % b); // 1

Explanation

  • % returns the remainder after division

  • 10 % 3 equals 1 because 3 × 3 = 9 with 1 left over

The modulus operator is commonly used for:

  • Checking even/odd numbers

  • Cyclic operations

Example:

let number = 6;

if (number % 2 === 0) {
  console.log("Even number");
}

Comparison Operators

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

Operator Description Example
== Equal (value only) 5 == "5"
=== Strict equal (value + type) 5 === "5"
!= Not equal 5 != 3
> Greater than 10 > 5
< Less than 3 < 7

The Difference Between == and ===

This is one of the most important concepts for beginners.

Example

console.log(5 == "5");  // true
console.log(5 === "5"); // false

Why?

Operator What It Checks
== Value only
=== Value and type

Explanation:

  • "5" is a string

  • 5 is a number

With ==, JavaScript converts types automatically.

With ===, JavaScript requires both value and type to match.

Best Practice:
Use === in most cases to avoid unexpected behavior.


Logical Operators

Logical operators combine or modify conditions.

Operator Meaning
&& AND
`
! NOT

Logical AND (&&)

Returns true only if both conditions are true.

let age = 25;
let hasLicense = true;

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

Both conditions must be true.


Logical OR (||)

Returns true if at least one condition is true.

let isWeekend = true;
let isHoliday = false;

if (isWeekend || isHoliday) {
  console.log("You can relax today");
}

Logical NOT (!)

Reverses a boolean value.

let isLoggedIn = false;

console.log(!isLoggedIn); // true

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 |


Assignment Operators

Assignment operators assign values to variables

The basic assignment operator is =

let x = 10;

JavaScript also supports short assignment operators

Operator Example Equivalent
= x = 5 Assign value
+= x += 3 x = x + 3
-= x -= 2 x = x - 2

Example

let score = 10;

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

score -= 3;
console.log(score); // 12

These operators make code shorter and easier to read


Practice Assignment

Try this small exercise to practise operators

1. Perform arithmetic operations.

let a = 12;
let b = 4;

console.log("Addition:", a + b);
console.log("Subtraction:", a - b);
console.log("Multiplication:", a * b);
console.log("Division:", a / b);

2. Compare Values

let x = 10;
let y = "10";

console.log(x == y);   // true
console.log(x === y);  // false

Observe how the results differ.


3. Logical Condition

let age = 20;
let hasTicket = true;

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

This condition checks two requirements.


Conclusion

JavaScript operators are the building blocks of everyday programming.

The most commonly used ones are:

  • Arithmetic operators for calculations

  • Comparison operators for evaluating values

  • Logical operators for building conditions

  • Assignment operators for updating variables

Understanding these basics will help you write clearer JavaScript code and prepare you for more advanced topics like control flow, functions, and data structures.

10 views