JavaScript

Operators in JavaScript with code examples

Operators in JavaScript with code examples

JavaScript is one of the most popular programming languages used for developing web applications. It is a high-level, dynamic, and interpreted language that is used to create interactive and dynamic web pages. JavaScript provides a wide range of operators that are used for performing various mathematical and logical operations. In this article, we will discuss the various types of operators in JavaScript along with code examples.

Arithmetic Operators

Arithmetic operators are used for performing mathematical operations such as addition, subtraction, multiplication, and division. The following list shows the arithmetic operators in JavaScript:

  • ( + ) Addition
  • ( - ) Subtraction
  • ( * ) Multiplication
  • ( / ) Division
  • ( % ) Modulus (Remainder after division)

Code Example:

let num1 = 10;
let num2 = 5;

// Addition
let sum = num1 + num2;
console.log(sum); // Output: 15

// Subtraction
let diff = num1 - num2;
console.log(diff); // Output: 5

// Multiplication
let product = num1 * num2;
console.log(product); // Output: 50

// Division
let quotient = num1 / num2;
console.log(quotient); // Output: 2

// Modulus
let remainder = num1 % num2;
console.log(remainder); // Output: 0

Bitwise Operators

Bitwise operators perform operations on the binary representation of a number. They are often used for low-level programming and manipulating data in a more efficient manner.

  • & Bitwise AND
  • | Bitwise OR
  • ^ Bitwise XOR
  • ~ Bitwise NOT
  • << Bitwise left shift
  • >> Bitwise right shift
  • >>> Bitwise unsigned right shift (shifts in zeros from the left side)
let a = 5;     // 0101
let b = 3;

let result = a & b;  // 0001
console.log(result); // Output: 1

let result = a | b;  // 0111
console.log(result); // Output: 7

let result = a ^ b;  // 0110
console.log(result); // Output: 6

let result = ~a;  // 1010 (equivalent to -6 in decimal)
console.log(result); // Output: -6

let result = a << 2;  // 010100 (equivalent to 20 in decimal)
console.log(result); // Output: 20

let result = a >> 2;  // 0001 (equivalent to 1 in decimal)
console.log(result); // Output: 1

Assignment Operators

Assignment operators are used for assigning values to variables. The following list shows the assignment operators in JavaScript:

  • ( = ) Assignment
  • ( += ) Addition and assignment
  • ( -= ) Subtraction and assignment
  • ( *= ) Multiplication and assignment
  • ( /= ) Division and assignment
  • ( %= ) Modulus and assignment

Code Example:

let num = 10;

// Assignment
num = 20;
console.log(num); // Output: 20

// Addition and assignment
num += 5;
console.log(num); // Output: 25

// Subtraction and assignment
num -= 5;
console.log(num); // Output: 20

// Multiplication and assignment
num *= 2;
console.log(num); // Output: 40

// Division and assignment
num /= 2;
console.log(num); // Output: 20

// Modulus and assignment
num %= 3;
console.log(num); // Output: 2

Comparison Operators

Comparison operators are used for comparing values. The following list shows the comparison operators in JavaScript:

  • ( == ) Equal to
  • ( === ) Equal to (strict)
  • ( != ) Not equal to
  • ( !== ) Not equal to (strict)
  • ( > ) Greater than
  • ( < ) Less than
  • ( >= ) Greater than or equal to
  • ( <= ) Less than or equal to

Code Example:

let num1 = 10;
let num2 = 5;

// Equal to
console.log(num1 == num2); // Output: false

// Not equal to
console.log(num1 != num2); // Output: true

// Greater than
console.log(num1 > num2); // Output: true

// Less than
console.log(num1 < num2); // Output: false

// Greater than or equal to
console.log(num1 >= num2); // Output: true

Increment & Decrement operators

The increment operator is used to increase the value of a variable by 1. It is denoted by the ++ symbol and can be placed either before or after the variable. If the operator is placed before the variable, then the value is incremented first and then the expression is evaluated. If the operator is placed after the variable, then the expression is evaluated first and then the value is incremented.

let num = 10;

// Post-increment
console.log(num++); // Output: 10
console.log(num);   // Output: 11

// Pre-increment
console.log(++num); // Output: 12
console.log(num);   // Output: 12

The decrement operator is used to decrease the value of a variable by 1. It is denoted by the -- symbol and can be placed either before or after the variable. If the operator is placed before the variable, then the value is decremented first and then the expression is evaluated. If the operator is placed after the variable, then the expression is evaluated first and then the value is decremented.

Code Example:

let num = 10;

// Post-decrement
console.log(num--); // Output: 10
console.log(num);   // Output: 9

// Pre-decrement
console.log(--num); // Output: 8
console.log(num);   // Output: 8

Logical Operators

Logical operators are used for performing logical operations such as AND, OR, and NOT. The following list shows the logical operators in JavaScript:

  • ( && ) AND
  • ( || ) OR
  • ( ! ) NOT

Code Example:

let num1 = 10;
let num2 = 5;

// AND
console.log(num1 > 5 && num2 < 10); // Output: true

// OR
console.log(num1 > 5 || num2 > 10); // Output: true

// NOT
console.log(!(num1 > 5));           // Output: false

Conditional (Ternary) Operator

The conditional operator is used to evaluate a condition and return a value based on the condition. It is denoted by the ? and : symbols and is also known as the ternary operator. The syntax for the conditional operator is as follows:

condition ? value1 : value2

If the condition is true, then value1 is returned. If the condition is false, then value2 is returned.

Code Example:

let num1 = 10;
let num2 = 5;

let result = (num1 > num2) ? "num1 is greater" : "num2 is greater";
console.log(result); // Output: num1 is greater

JavaScript provides a wide range of operators that are used for performing various mathematical and logical operations. It is important to have a good understanding of these operators in order to write efficient and effective JavaScript code.

A blog for self-taught engineers

Сommunity is filled with like-minded individuals who are passionate about learning and growing as engineers.