Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

Updated
4 min read

JavaScript operators are an important part of programming. Using operators, we can perform different operations and write conditions in our code.

Operators are mainly divided into four common types:

  1. Assignment operators (=, +=, -=)

  2. Logical operators (&&, ||, !)

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

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

Let's look at them one by one.

Assignment Operators

Assignment operators are used to assign values to variables.

= operator

The = operator assigns a value to a variable

let x = 4

console.log(x)//8

+= operator

+= adds a value to the existing variable value.

let x=3

//same as x= x + 5
let x+=5

console.log(x)//8

-= operator

-= subtracts a value from the existing variable value.

let x=3

// same as x = x - 5
x -= 5;

console.log(x)//-2

Logical Operators

Logical operators are used when we need to check multiple conditions.

&& (AND operator)

&& returns true only if both conditions are true.

let a = 4;

if (a == 4 && a < 10) {
  console.log("Both conditions are true");
}

|| (OR operator)

|| returns true if any one condition is true.

let a = 4;

if (a == 4 || a == 5) {
  console.log("One condition is true");
}

! (NOT operator)

! is used to negate a value.

let a = 0;

if (!a) {
  console.log("a has a falsy value");
}

Comparison Operators

Comparison operators are used to compare two values.

== (Loose equality)

== compares values only, not their types.

if (4 == "4") {
  console.log("true");
}

=== (Strict equality)

=== compares both value and type.

if (4 === "4") {
  console.log("true");
} else {
  console.log("false");
}

!= (Not equal)

if (4 != 6) {
  console.log("true");
}

< and <=

let a = 4;

if (a < 4) {
  console.log("less than");
}

if (a <= 4) {
  console.log("less than or equal");
}

> and >=

let b = 99;

if (b > 99) {
  console.log("greater");
}

if (b >= 99) {
  console.log("greater or equal");
}

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations.

Addition (+)

let x = 6;
let y = 6;

let sum = x + y;

console.log(sum); // 12

The + operator can also concatenate strings.

let a = "abc";
let b = "def";

let wordSum = a + b;

console.log(wordSum); // abcdef

Subtraction (-)

The - operator is used to subtract one number from another.

let a = 10;
let b = 4;

let result = a - b;

console.log(result); // 6

Multiplication (*)

The * operator is used to multiply numbers.

let a = 5;
let b = 3;

let result = a * b;

console.log(result); // 15

Division (/)

The / operator is used to divide numbers.

let a = 20;
let b = 4;

let result = a / b;

console.log(result); // 5

Modulus (%)

The % operator returns the remainder of a division.

let a = 10;
let b = 3;

let result = a % b;

console.log(result); // 1

This operator is commonly used to check if a number is even or odd.

let num = 6;

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

Increment (++)

The ++ operator increases the value of a variable by 1.

let x = 5;

//This is the same as: x = x + 1;

x++;

console.log(x); // 6

Decrement (--)

The -- operator decreases the value of a variable by 1.

let x = 5;
//This is the same as: x = x - 1;
x--;

console.log(x); // 4