Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators : The Basics You Need to Know

Updated
JavaScript Operators : The Basics You Need to Know
M

I believe writing makes learning easier so I share simple Tech notes with diagrams

Introduction -

In this blog not in complicated structure but in simple way, we will explore some of the most commonly used JavaScript operators with simple examples.

When we start learning JavaScript one of the first things we encounter is operators.
Operators are simple but powerful because they allow us to perform calculations, compare values, and make decisions in our programs. If you've ever done basic math like 5 + 3 you have already used an operator. In programming, operators work in a similar way they tell the computer what action should be performed between values.

What Are Operators?

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

For example :

let a = 10;
let b = 5;

console.log(a + b);

Here the + operator tells JavaScript to add the two numbers.

Operators help us perform tasks such as :

  • Doing calculations

  • Comparing values

  • Combining conditions

  • Updating variable values

Arithmetic Operators -

Arithmetic operators are used for basic mathematical calculations.
Example :

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

The % operator returns the remainder after division.

For example :

10 ÷ 3 = remainder 1

So :

console.log(10 % 3); // 1

Comparison Operators -

Comparison operators are used to compare two values.
They always return a boolean value (true or false).

Example :

let x = 5;
let y = "5";

console.log(x == y);

Output :

true

This happens because == only compares values, not types. Now look at this :

console.log(x === y);

Output :

false

The === operator checks both value and data type.

5 → number
"5" → string

Because the types are different JavaScript returns false. For this reason, developers usually prefer using === instead of ==.

Logical Operators -

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

Example :

let age = 20;
let hasTicket = true;

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

Here && means both conditions must be true.

Truth Table

Example using OR :

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

This works if any one condition is true.

Assignment Operators -

Assignment operators are used to assign values to variables. The most common assignment operator is =. These operators help keep code shorter and cleaner.

Example :

let score = 10;

We can also update values using shorthand operators.

Operator Meaning
= assign value
+= add and assign
-= subtract and assign

Example :

let score = 10;

score += 5;

console.log(score);

This is equivalent to writing :

score = score + 5;

Simple Practice Exercise -

Try the following small exercise in your console, it will help you to understand things by experiencing it.

1. Perform arithmetic operations

let a = 8;
let b = 4;

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

2. Compare values

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

3. Use logical operators

let age = 20;

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

Practicing these small examples will help you understand how operators work in real programs, let me know the output.

Conclusion -

Operators are one of the fundamental building blocks of JavaScript. They allow us to perform calculations, compare values, and control how our programs behave. Understanding arithmetic, comparison, logical, and assignment operators gives a strong foundation for writing JavaScript code. Once these basics become comfortable it becomes much easier to work with conditions, loops, and functions.

JavaScript Fundamentals Series

Part 1 of 8

In this JavaScript series we are going to cover these topics - 1 - Understanding Variables and Data Types in JavaScript 2 - JavaScript Operators: The Basics You Need to Know 3 - Control Flow in JavaScript: If, Else, and Switch Explained 4 - Function Declaration vs Function Expression: What’s the Difference? 5 - Arrow Functions in JavaScript: A Simpler Way to Write Functions 6 - JavaScript Arrays 101 7 - Array Methods You Must Know 8 - Understanding Objects in JavaScript 9 - Understanding Object-Oriented Programming in JavaScript 10 - The Magic of this, call(), apply(), and bind() in JavaScript

Up next

Understanding Variables and Data Types in JavaScript

Introduction - When learning JavaScript one of the first concepts we encounter is variables. Variables are essential because they allow our programs to store and manage information. Before starting co