Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators (JS 101)

JS Operators in perhaps a new light.

Updated
4 min read
JavaScript Operators (JS 101)

The hardest parts are explaining the fundamental stuff. Explaining complex things while having the knowledge of the fundamental concepts is fairly easy when compared to the former.

  • Operators are the most common components of any programming language.

  • When you are writing code, you are constantly working with data—calculating totals, checking if a user is logged in, or updating a counter. Operators are the special symbols that let us actually do things with that data.

1. Arithmetic Operators (+, -, *, /, %)

These do exactly what you would expect from basic math. They allow you to perform calculations on numbers.

  • + Addition

  • - Subtraction

  • * Multiplication

  • / Division

  • % Modulus (Returns the remainder of a division)

Everyday Example:

Imagine you are building a system to manage a collection of books.

JavaScript

let fictionBooks = 15;
let nonFictionBooks = 10;

// Addition
let totalBooks = fictionBooks + nonFictionBooks;
console.log(totalBooks); // Output: 25

// Subtraction
let booksRemaining = totalBooks - 3; // 3 books were checked out
console.log(booksRemaining); // Output: 22

// Modulus (Great for finding even/odd numbers)
let remainder = 10 % 3; 
console.log(remainder); // Output: 1 (Because 10 divided by 3 is 9, with 1 left over)

2. Assignment Operators (=, +=, -=)

You use assignment operators to assign values to your variables.

  • = The basic assignment operator. It assigns the value on the right to the variable on the left.

  • += Adds the right value to the variable and assigns the new result.

  • -= Subtracts the right value from the variable and assigns the new result.

Everyday Example:

JavaScript

let currentScore = 50; // Basic assignment

// The player scores 10 points
currentScore += 10; // This is a shortcut for: currentScore = currentScore + 10
console.log(currentScore); // Output: 60

// The player takes a penalty
currentScore -= 5;
console.log(currentScore); // Output: 55

3. Comparison Operators (==, ===, !=, >, <)

These operators compare two values and always return a boolean (true or false). They are the backbone of decision-making in your code.

  • > Greater than

  • < Less than

  • == Loose equality (Checks if values are the same)

  • === Strict equality (Checks if values AND data types are the same)

  • != Not equal to

The one extra '=': == vs ===

If you only take one thing away from this post, make it this: always use === instead of ==.

The loose equality (==) tries to be "helpful" by converting data types before comparing them, which leads to weird bugs. Strict equality (===) checks exactly what is there.

JavaScript

let age = 25;      //Number
let ageString = "25"; //String

// Loose equality (==)
console.log(age == ageString); 
// Output: true (JavaScript converts the string to a number behind the scenes)

// Strict equality (===)
console.log(age === ageString); 
// Output: false (Different data types, so they are not strictly equal)

4. Logical Operators (&&, ||, !)

Logical operators are used to combine multiple conditions together.

truth table for logical operators AND OR NOT, AI generated
  • && (AND): Returns true only if both sides are true.

  • || (OR): Returns true if at least one side is true.

  • ! (NOT): Flips the boolean value (true becomes false, false becomes true).

Everyday Example:

JavaScript

let hasTicket = true;
let isVIP = false;

// && (AND) - Needs both to be true
console.log(hasTicket && isVIP); // Output: false

// || (OR) - Needs only one to be true
console.log(hasTicket || isVIP); // Output: true

// ! (NOT) - Reverses the truth
let isBanned = false;
console.log(!isBanned); // Output: true (They are NOT banned)

Operator table

Category

Operators

Description

Arithmetic

+, -, *, /, %

Perform basic mathematical calculations on numbers.

Assignment

=, +=, -=

Assign or update values stored in variables.

Comparison

==, ===, !=, >, <

Compare two values to make decisions (returns true or false).

Logical

&&, ||, !

Are used to perform operations on boolean

Hope you learnt some fundamental topics. I part with a wish of a beautiful summer.

I extend my gratitude towards the #chaicode team. Learnt so much in so little time.