DEV Community

Cover image for JavaScript Operators: The Basics You Need to Know
Kunal
Kunal

Posted on

JavaScript Operators: The Basics You Need to Know

This is the one of the very first step in programming every beginner start from this topic either perform calculations , some compression or something logical you need these

In this blog we will cover :

  • What operators are
  • Arithmetic operators (+, -, *, /, %)
  • Comparison operators (==, ===, !=, >, <)
  • Logical operators (&&, ||, !)
  • Assignment operators (=, +=, -=)

Operators

In JavaScript , operators is a special symbol that is used to do some operations on value , These value are also known as operand

Lets understand this with some example :


const a = 10
const b = 20

const result = a + b 
console.log(result) // 30
Enter fullscreen mode Exit fullscreen mode

Here above + symbol is the operator and the values a and b are the operand.
The operator tell the computer to add the values together.

There are several type of operators let look at each of them one by one


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

These are the operators which are used to do some arithmetic operations.
Which you have learned in school for the basic math.


| Operator | Name            | Description                              | Example  | Result |
|----------|-----------------|------------------------------------------|----------|--------|
| +        | Addition        | Adds two numbers                         | a + b    | 14     |
| -        | Subtraction     | Subtracts one number from another        | a - b    | 6      |
| *        | Multiplication  | Multiplies two numbers                   | a * b    | 40     |
| /        | Division        | Divides one number by another            | a / b    | 2.5    |
| %        | Modulo          | Gives the remainder of a division        | a % b    | 2      |
| **       | Exponentiation  | Raises the first number to power second  | a ** b   | 10000  |

Enter fullscreen mode Exit fullscreen mode

Lets understand this with some example


const chocolates = 23
const kids = 5

let leftover = chocolates % kids
console.log(leftover) // 3
Enter fullscreen mode Exit fullscreen mode
  1. Comparison operators (==, ===, !=, >, <)

Comparison operators are used to compare two value.


| Operator | Name                 | Description                          | Example      | Result |
|----------|----------------------|--------------------------------------|--------------|--------|
| ==       | Equal to             | Compares values only                 | 10 == "10"   | true   |
| ===      | Strict equal to      | Compares value and type              | 10 === "10"  | false  |
| !=       | Not equal to         | Checks if values are not equal       | 10 != "10"   | false  |
| >        | Greater than         | Checks if left is greater than right | 10 > 5       | true   |
| <        | Less than            | Checks if left is less than right    | 10 < 5       | false  |
Enter fullscreen mode Exit fullscreen mode

Lets understand this with some example


// Strict equality (recommended)
console.log(50 === 50);     // true  (same value, same type)
console.log(50 === "50");   // false (number vs string)

// Loose equality (confusing)
console.log(50 == "50");    // true (type coercion happens)
Enter fullscreen mode Exit fullscreen mode
  1. Logical operators (&&, ||, !)

Logical operators are used to combine two or more true/false conditions.

| Operator | Name         | Description                                              |
|----------|--------------|----------------------------------------------------------|
| &&       | Logical AND  | Returns true only if both conditions are true            |
| ||       | Logical OR   | Returns true if at least one condition is true           |
| !        | Logical NOT  | Reverses the boolean value (true  false)                |
Enter fullscreen mode Exit fullscreen mode

Lets understand this with some example :


// Logical Operators Example

let isLoggedIn = true;
let isPremiumUser = false;

// Access premium content → need both
let canAccessPremium = isLoggedIn && isPremiumUser;
console.log(canAccessPremium); // false

// Show basic content → need at least one
let canAccessBasic = isLoggedIn || isPremiumUser;
console.log(canAccessBasic); // true

// Reverse a condition
let isGuest = !isLoggedIn;
console.log(isGuest); // false
Enter fullscreen mode Exit fullscreen mode
  1. Assignment operators (=, +=, -=)

These operators are used to assign values to variables. The most common one is the equals sign (=).


| Operator | Example   | Same As     |
|----------|-----------|-------------|
| +=       | x += y    | x = x + y   |
| -=       | x -= y    | x = x - y   |
| *=       | x *= y    | x = x * y   |
| /=       | x /= y    | x = x / y   |

Enter fullscreen mode Exit fullscreen mode

Lets take some example


// Assignment Operator Example

let balance = 500; // initial balance

// Spend 150
balance -= 150; // same as: balance = balance - 150

console.log(balance); // 350
Enter fullscreen mode Exit fullscreen mode

Thanks for reading ! if enjoyed this blog , you can read more on this 👇

Top comments (0)