Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What are Operators in JavaScript?

JavaScript is an essential language for web development, allowing us to create interactive web experiences. One of the fundamental concepts in JavaScript, and programming in general, is the concept of operators. In this article, we'll explore what operators are, the different types of operators in JavaScript, and how to use them effectively.

What are Operators?

Operators in JavaScript, and in programming languages in general, are symbols that perform specific operations on values, called operands. Think of operators as the "verbs" of the programming world. They carry out actions on values, like adding two numbers together or comparing if two values are equal.

For example, consider the following line of code:

let sum = 5 + 3;

In this example, 5 and 3 are the operands, and + is the operator that adds them together. The result of this operation, 8, is then assigned to the variable sum.

Operators play a crucial role in JavaScript. They help us manipulate data, perform calculations, and make decisions based on conditions. There are different types of operators in JavaScript, each with its own purpose and use case.

Types of Operators in JavaScript

JavaScript provides a wide range of operators, which can be grouped into the following categories:

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. String Operators
  6. Conditional (Ternary) Operator
  7. Type Operators
  8. Bitwise Operators

Let's explore each of these categories in detail, along with examples.

1. Arithmetic Operators

Arithmetic operators perform mathematical operations like addition, subtraction, multiplication, and division. Here are the most common arithmetic operators in JavaScript:

  • Addition (+): Adds two numbers together.

javascript let a = 5 + 3; // a is now 8

  • Subtraction (-): Subtracts the right operand from the left operand.

javascript let b = 5 - 3; // b is now 2

  • Multiplication (*): Multiplies two numbers.

javascript let c = 5 * 3; // c is now 15

  • Division (/): Divides the left operand by the right operand.

javascript let d = 15 / 3; // d is now 5

  • Remainder (%): Returns the remainder of a division operation.

javascript let e = 7 % 3; // e is now 1 because 7 divided by 3 leaves a remainder of 1

  • Exponentiation (**): Raises the left operand to the power of the right operand.

javascript let f = 2 ** 3; // f is now 8 because 2 raised to the power of 3 is 8

  • Increment (++): Adds 1 to a variable.

javascript let g = 5; g++; // g is now 6

  • Decrement (--): Subtracts 1 from a variable.

javascript let h = 5; h--; // h is now 4

These operators can be used to perform complex calculations and manipulate numerical values in your programs.

2. Assignment Operators

Assignment operators are used to assign values to variables. The most basic assignment operator is the equal sign (=), which assigns the value on the right side of the operator to the variable on the left side.

let i = 10; // i is now 10

In addition to the basic assignment operator, JavaScript provides several compound assignment operators that perform an arithmetic operation and assignment in a single step:

  • Addition assignment (+=): Adds the right operand to the left operand and assigns the result to the left operand.

javascript let j = 5; j += 3; // j is now 8 (equivalent to j = j + 3)

  • Subtraction assignment (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.

javascript let k = 5; k -= 3; // k is now 2 (equivalent to k = k - 3)

  • Multiplication assignment (*=): Multiplies the left operand by the right operand and assigns the result to the left operand.

javascript let l = 5; l *= 3; // l is now 15 (equivalent to l = l * 3)

  • Division assignment (/=): Divides the left operand by the right operand and assigns the result to the left operand.

javascript let m = 15; m /= 3; // m is now 5 (equivalent to m = m / 3)

  • Remainder assignment (%=): Finds the remainder of dividing the left operand by the right operand and assigns the result to the left operand.

javascript let n = 7; n %= 3; // n is now 1 (equivalent to n = n % 3)

  • Exponentiation assignment (**=): Raises the left operand to the power of the right operand and assigns the result to the left operand.

javascript let o = 2; o **= 3; // o is now 8 (equivalent to o = o ** 3)

These compound assignment operators can make your code shorter and easier to read.

3. Comparison Operators

Comparison operators compare two values and return a boolean value (true or false) based on the result of the comparison. These operators are essential for making decisions in your programs using conditional statements like if, else, and switch.

Here are the most common comparison operators in JavaScript:

  • Equal (==): Returns true if the operands are equal.

javascript let p = 5 == 3; // p is now false

  • Not equal (!=): Returns true if the operands are not equal.

javascript let q = 5 != 3; // q is now true

  • Strictly equal (===): Returns true if the operands are equal and of the same type.

javascript let r = 5 === "5"; // r is now false because the type of 5 is not the same as the type of "5"

  • Strictly not equal (!==): Returns true if the operands are not equal or not of the same type.