Back to blogs

Mastering JavaScript Operators - Your Ultimate Reference Guide

By Harshit Kumar | 2023-11-14

10 min read javascript

JavaScript Array Image

Welcome to the world of JavaScript operators, where every line of code starts with a decision. Operators are the backbone of any programming language, and in JavaScript, they are the tools that empower you to perform a wide variety of operations, from simple arithmetic to complex logical manipulations.

This blog post is your gateway to understanding and harnessing the power of JavaScript operators. Whether you're a newbie taking your first steps into coding or an experienced developer looking to hone your skills, this exploration will equip you with the knowledge to navigate the complex world of operators.

Arithmetic Operators

Arithmetic operators in JavaScript perform mathematical calculations, including addition, subtraction, multiplication, division, and more.

01. Addition (+)

The addition operator (+) in JavaScript is used to perform arithmetic addition when applied to numeric values or to concatenate strings when used with strings.

For Numeric Addition

const a = 5;
const b = 10;

const result = a + b; // result will be 15

For String Concatenation

const string1 = "Hello, ";
const string2 = "world!";

const message = string1 + string2; // message will be "Hello, world!"

When used with strings, the addition operator concatenates the strings together, joining the contents of string1 and string2 to form a new string "Hello, world!".

It's important to note that the behavior of the + operator can change based on the types of the operands. When used with numbers, it performs addition. When used with strings or a combination of string and numbers, it concatenates the values into a new string.

Mixing Number and String

const num = 5;
const text = "Apples";

const mix = num + text; // mix will be "5Apples"

When combining a number and a string using the + operator, JavaScript coerces the number to a string and concatenates it with the other string. This is called implicit type conversion or type coercion.

It's essential to be cautious when using the + operator, especially when working with different data types, to avoid unexpected concatenation or addition results due to type coercion.

02. Subtraction (-)

The subtraction operator (-) in JavaScript is used for arithmetic subtraction, allowing us to subtract one numeric value from another.

Numeric Subtraction

const a = 10;
const b = 5;

const result = a - b; // result will be 5

Applying the Subtraction Operator to Variables

We can use variables as operands with the subtraction operator, making it a useful tool for performing dynamic calculations based on changing values.

Floats and Negative Numbers

const x = -7;
const y = 3.5;

const difference = x - y; // difference will be -10.5

Keep in mind that the result of subtracting two numbers can be negative if the second number is greater than the first.

03. Multiplication (*)

The multiplication operator (*) in JavaScript is used for arithmetic multiplication, allowing us to multiply one numeric value by another.

Numeric Multiplication

const a = 5;
const b = 4;

const result = a * b; // result will be 20

Applying the Multiplication Operator to Variables

We can use variables as operands with the multiplication operator, making it a useful tool for performing dynamic calculations based on changing values.

Floats and Negative Numbers

const x = -3;
const y = 2.5;

const product = x * y; // product will be -7.5

The result of multiplying two numbers can be negative if one or both of the numbers are negative. Additionally, floating-point numbers can be used, resulting in accurate multiplication of non-integer values.

04. Division (/)

The division operator (/) in JavaScript is used for arithmetic division, allowing us to divide one numeric value by another.

Numeric Division

const a = 20;
const b = 5;

const result = a / b; // result will be 4

Applying the Division Operator to Variables

We can use variables as operands with the division operator, making it a useful tool for performing dynamic calculations based on changing values.

Floats and Negative Numbers

const x = -10;
const y = 2.5;

const quotient = x / y; // quotient will be -4

The result of dividing two numbers can be negative if one or both of the numbers are negative. Additionally, floating-point numbers can be used, resulting in accurate division of non-integer values.

Handling Division by Zero

It's important to note that dividing by zero (0) is not allowed in JavaScript and will result in a special value called "Infinity" or "-Infinity," depending on the sign of the divisor.

const zero = 0;

const result = 5 / zero; // result will be Infinity

05. Modulus (%)

The modulus operator (%) in JavaScript is used for arithmetic modulus, which calculates the remainder when one numeric value is divided by another. It returns the remainder of the division operation.

Numeric Modulus

const a = 10;
const b = 3;

const remainder = a % b; // remainder will be 1

Modulus for Even and Odd Numbers

The modulus operator is often used to determine whether a number is even or odd. When a number is divided by 2, if the remainder is 0, it's an even number; if the remainder is 1, it's an odd number.

const num = 7;

const isEven = num % 2 === 0; // isEven will be false (7 is odd)

Handling Division by Zero

Just like the division operator, you should be cautious when using the modulus operator to avoid division by zero. Dividing by zero using the modulus operator will result in "NaN" (Not-a-Number) or other unexpected behavior.

06. Exponentiation (**)

The exponentiation operator (**) in JavaScript is used to raise a base number to a specified exponent. It calculates the power or exponentiation of a numeric value.

Numeric Exponentiation

const base = 2;
const exponent = 3;

const result = base ** exponent; // result will be 8

Floats and Negative Exponents

const base = 4;
const exponent = -2;

const result = base ** exponent; // result will be 0.0625

07. Increment (++) - Pre and Post

In JavaScript, the increment operator (++) is used to increase the value of a variable by 1. It can be applied in two different ways: as a pre-increment operator and as a post-increment operator.

Pre-Increment (++) Operator

The pre-increment operator (++variable) is used before a variable, and it increases the value of the variable by 1 before the current value is used in an expression.

let x = 5;

let y = ++x; // y will be 6, and x will also be 6

In this example, ++x first increments the value of x from 5 to 6, and then the resulting value (6) is assigned to y. Both x and y end up with the value 6.

Post-Increment (++) Operator

let a = 5;

let b = a++; // b will be 5, and a will be 6

In this example, a++ assigns the current value of a (5) to b, and then it increments a from 5 to 6.

Things to remember

  • The choice between pre-increment and post-increment depends on the specific behavior we need in your code. Pre-increment is used when we want to increment the variable before using its value, while post-increment is used when we want to use the current value before incrementing it.

08. Decrement (--) - Pre and Post

In JavaScript, the decrement operator (--) is used to decrease the value of a variable by 1. It can be applied in two different ways: as a pre-decrement operator and as a post-decrement operator.

Pre-Decrement (--) Operator

The pre-decrement operator (--variable) is used before a variable, and it decreases the value of the variable by 1 before the current value is used in an expression.

let x = 5;

let y = --x; // y will be 4, and x will also be 4

Post-Decrement (--) Operator

The post-decrement operator (variable--) is used after a variable, and it decreases the value of the variable by 1 after the current value is used in an expression.

let a = 5;

let b = a--; // b will be 5, and a will be 4

Comparison Operators:

Comparison operators in JavaScript are used to compare values and determine the relationship between them, returning either true or false. They include ==, !=, ===, !==, >, <, >=, and <=.

01. Equal to (==)

The equal to (==) operator in JavaScript is used for equality comparison between two values. It checks whether the values on both sides of the operator are equal or equivalent, regardless of their data types.

Equality Comparison

When we use the == operator, JavaScript performs type coercion, which means it converts one or both values to a common data type before making the comparison.

const num1 = 5;
const num2 = "5";

if (num1 == num2) {
  console.log("They are equal");
} else {
  console.log("They are not equal");
}

// Output: They are equal

In this example, num1 is a numeric value, and num2 is a string that looks like a number. Despite the difference in data types, the == operator considers them equal because it performs type coercion and converts num2 to a number before making the comparison.

Usage Considerations

While the == operator can be convenient for loose equality comparisons, it can lead to unexpected results when comparing values of different data types.

02. Not Equal to (!=)

The "Not Equal to" (!=) operator in JavaScript is used to check if two values are not equal or not equivalent. It returns true if the values on both sides of the operator are not equal, and false if they are equal.

Inequality Comparison

The != operator checks if the values are not equal, regardless of their data types. It is often used when we want to determine whether two values are different from each other.

const num1 = 5;
const num2 = "5";

if (num1 != num2) {
  console.log("They are not equal");
} else {
  console.log("They are equal");
}

// Output: They are equal

The != operator is used when you want to check for inequality without considering data types.

03. Strict Equal to (===)

The "Strict Equal to" (===) operator in JavaScript is used for strict equality comparison between two values. It checks whether the values on both sides of the operator are not only equal in content but also have the same data type.

Strict Equality Comparison

When we use the === operator, JavaScript performs a strict comparison without type coercion, meaning it checks both value and data type equality.

const num1 = 5;
const num2 = "5";

if (num1 === num2) {
  console.log("They are strictly equal");
} else {
  console.log("They are not strictly equal");
}

// Output: They are not strictly equal

In this example, num1 is a numeric value, and num2 is a string that looks like a number. The === operator considers them not strictly equal because they have different data types.

Usage Considerations

The === operator is used when we want to ensure both value and data type consistency in our comparison. It is recommended for most equality checks to avoid unexpected results due to type coercion.

04. Strict Not Equal to (!==)

The "Strict Not Equal to" (!==) operator in JavaScript is used for strict inequality comparison between two values. It checks whether the values on both sides of the operator are not equal in content or have different data types.

Strict Inequality Comparison

When you use the !== operator, JavaScript performs a strict comparison without type coercion, meaning it checks both value and data type inequality.

const num1 = 5;
const num2 = "5";

if (num1 !== num2) {
  console.log("They are strictly not equal");
} else {
  console.log("They are strictly equal");
}

// Output: They  are strictly not equal

In this example, num1 is a numeric value, and num2 is a string that looks like a number. The !== operator considers them strictly not equal because they have different data types.

05. Greater than (>)

The "Greater than" (>) operator in JavaScript is used to compare two values to determine if the value on the left is greater than the value on the right. It returns true if the value on the left is greater, and false if it is not.

const num1 = 10;
const num2 = 5;

if (num1 > num2) {
  console.log("num1 is greater than num2");
} else {
  console.log("num1 is not greater than num2");
}

// Output: num1 is greater than num2

In this example, num1 is compared to num2 using the > operator. Since num1 (10) is greater than num2 (5), the condition is met, and "num1 is greater than num2" is displayed.

Usage Considerations

  • The > operator is commonly used for comparing numeric values, such as integers or floating-point numbers, to determine which value is larger.
  • It can also be used for comparing strings, where the comparison is based on lexicographic (dictionary) order. In this case, it checks whether one string is "greater" (comes after) another in alphabetical order.

06. Less than (<)

The "Less than" (<) operator in JavaScript is used to compare two values to determine if the value on the left is less than the value on the right. It returns true if the value on the left is less, and false if it is not.

const num1 = 5;
const num2 = 10;

if (num1 < num2) {
  console.log("num1 is less than num2");
} else {
  console.log("num1 is not less than num2");
}

// Output : num1 is less than num2

In this example, num1 is compared to num2 using the < operator. Since num1 (5) is less than num2 (10), the condition is met, and "num1 is less than num2" is displayed.

The < operator is commonly used for comparing numeric values, such as integers or floating-point numbers, to determine which value is smaller.

It can also be used for comparing strings, where the comparison is based on lexicographic (dictionary) order. In this case, it checks whether one string is "less" (comes before) another in alphabetical order.

07. Greater than or Equal to (>=)

The "Greater than or Equal to" (>=) operator in JavaScript is used to compare two values to determine if the value on the left is greater than or equal to the value on the right. It returns true if the value on the left is greater than or equal to the value on the right, and false if it is not.

const num1 = 10;
const num2 = 5;

if (num1 >= num2) {
  console.log("num1 is greater than or equal to num2");
} else {
  console.log("num1 is not greater than or equal to num2");
}

// Output: num1 is greater than or equal to num2

In this example, num1 is compared to num2 using the >= operator. Since num1 (10) is greater than num2 (5), the condition is met, and "num1 is greater than or equal to num2" is displayed.

08. Less than or Equal to (<=)

The "Less than or Equal to" (<=) operator in JavaScript is used to compare two values to determine if the value on the left is less than or equal to the value on the right. It returns true if the value on the left is less than or equal to the value on the right, and false if it is not.

const num1 = 5;
const num2 = 10;

if (num1 <= num2) {
  console.log("num1 is less than or equal to num2");
} else {
  console.log("num1 is not less than or equal to num2");
}

// Output: num1 is less than or equal to num2

In this example, num1 is compared to num2 using the <= operator. Since num1 (5) is less than num2 (10), the condition is met, and "num1 is less than or equal to num2" is displayed.

Logical Operators:

Logical operators in JavaScript allow you to combine or modify Boolean values, offering && (logical AND), || (logical OR), and ! (logical NOT) for making complex decisions and performing Boolean algebra.

01. Logical AND (&&)

The "Logical AND" (&&) operator in JavaScript is used to perform a logical conjunction between two values or expressions. It returns true if both the value on the left and the value on the right are true, and false otherwise.

const isTrue = true;
const isFalse = false;

if (isTrue && isFalse) {
  console.log("Both values are true.");
} else {
  console.log("At least one value is false.");
}

// Output: At least one value is false

In this example, the && operator is used to check if both isTrue and isFalse are true. Since isFalse is false, the condition is not met, and "At least one value is false" is displayed.

The && operator returns true only if both values or expressions on the left and right are true.

If at least one of the values or expressions is false, the && operator returns false.

Short-Circuiting

The && operator supports short-circuiting. If the value on the left is false, the value on the right is not evaluated because the overall result will always be false. This can be useful for optimizing code and avoiding unnecessary computations.

02. Logical OR (||)

The "Logical OR" (||) operator in JavaScript is used to perform a logical disjunction between two values or expressions. It returns true if at least one of the values on the left or right is true, and false only if both values are false.

const isTrue = true;
const isFalse = false;

if (isTrue || isFalse) {
  console.log("At least one value is true.");
} else {
  console.log("Both values are false.");
}

// Output: At least one value is true.

In this example, the || operator is used to check if either isTrue or isFalse is true. Since isTrue is true, the condition is met, and "At least one value is true" is displayed.

The || operator returns true if at least one of the values or expressions on the left and right is true.

If both values or expressions are false, the || operator returns false.

Short-Circuiting

The || operator supports short-circuiting. If the value on the left is true, the value on the right is not evaluated because the overall result will always be true. This can be useful for optimizing code and avoiding unnecessary computations.

03. Logical NOT (!)

The "Logical NOT" (!) operator in JavaScript is used to perform logical negation on a value or expression. It returns true if the value is false, and false if the value is true.

const isTrue = true;
const isFalse = false;

console.log(!isTrue); // Outputs: false
console.log(!isFalse); // Outputs: true

The ! operator is commonly used to invert the logical value of a condition. It's often used in conditional statements to check for the opposite of a condition.

Assignment Operators:

Assignment operators in JavaScript are shortcuts for assigning values to variables, and they include =, +=, -=, *=, /=, %=, **=, <<=, >>=, >>>=, &=, |=, and ^=.

01. Assignment (=)

The "Assignment" (=) operator in JavaScript is used to assign a value to a variable. It takes the value on its right-hand side and stores it in the variable on its left-hand side. In other words, it assigns the value of the expression on the right to the variable on the left.

let x; // Declare a variable x

x = 10; // Assign the value 10 to the variable x

In this example, we declare a variable x and then use the = operator to assign the value 10 to it. After this assignment, the variable x holds the value 10.

Variable Initialization

In many cases, the assignment operator is used for initializing variables when they are declared.

let y = 20; // Declare a variable y and assign the value 20 to it

Reassignment

The = operator can also be used to reassign a new value to an existing variable.

let z = 30; // Declare a variable z and assign the value 30 to it

z = 40; // Reassign the value 40 to the variable z

02. Add and Assign (+=)

The "Add and Assign" (+=) operator in JavaScript is a compound assignment operator that is used to add the value on its right-hand side to the current value of a variable and then assign the result back to that variable. It effectively combines addition and assignment in a single operation.

let x = 5;
x += 3; // This is equivalent to x = x + 3;

console.log(x); // Outputs: 8

In this example, we have a variable x initially set to 5. When we use the += operator, it adds 3 to the current value of x, resulting in 8. The final value is stored back in the variable x.

The += operator can also be used to concatenate strings. For example, you can use it to append a string to an existing string:

let message = "Hello, ";
message += "world!";

console.log(message); // Outputs: "Hello, world!"

03. Subtract and Assign (-=)

The "Subtract and Assign" (-=) operator in JavaScript is a compound assignment operator that is used to subtract the value on its right-hand side from the current value of a variable and then assign the result back to that variable. It effectively combines subtraction and assignment in a single operation.

let x = 10;
x -= 5; // This is equivalent to x = x - 5;

console.log(x); // Outputs: 5

In this example, we have a variable x initially set to 10. When we use the -= operator, it subtracts 5 from the current value of x, resulting in 5. The final value is stored back in the variable x.

04. Multiply and Assign (*=)

The "Multiply and Assign" (*=) operator in JavaScript is a compound assignment operator that is used to multiply the value on its right-hand side by the current value of a variable and then assign the result back to that variable. It effectively combines multiplication and assignment in a single operation.

let x = 3;
x *= 4; // This is equivalent to x = x * 4;

console.log(x); // Outputs: 12

In this example, we have a variable x initially set to 3. When we use the *= operator, it multiplies the current value of x by 4, resulting in 12. The final value is stored back in the variable x.

05. Divide and Assign (/=)

The "Divide and Assign" (/=) operator in JavaScript is a compound assignment operator that is used to divide the current value of a variable by the value on its right-hand side and then assign the result back to that variable. It effectively combines division and assignment in a single operation.

let x = 12;
x /= 3; // This is equivalent to x = x / 3;

console.log(x); // Outputs: 4

In this example, we have a variable x initially set to 12. When we use the /= operator, it divides the current value of x by 3, resulting in 4. The final value is stored back in the variable x.

06. Modulus and Assign (%=)

The "Modulus and Assign" (%=) operator in JavaScript is a compound assignment operator that is used to find the remainder when dividing the current value of a variable by the value on its right-hand side, and then assign the result back to that variable. It effectively combines the modulus operation and assignment in a single step.

let x = 17;
x %= 5; // This is equivalent to x = x % 5;

console.log(x); // Outputs: 2

In this example, we have a variable x initially set to 17. When we use the %= operator, it calculates the remainder when dividing the current value of x by 5, resulting in 2. The final value is stored back in the variable x.

07. Exponentiation and Assign (**=)

The "Exponentiation and Assign" (**=) operator in JavaScript is a compound assignment operator that is used to raise the current value of a variable to the power of the value on its right-hand side and then assign the result back to that variable. It effectively combines exponentiation and assignment in a single operation.

let x = 2;
x **= 3; // This is equivalent to x = x ** 3;

console.log(x); // Outputs: 8

In this example, we have a variable x initially set to 2. When we use the **= operator, it raises the current value of x to the power of 3, resulting in 8. The final value is stored back in the variable x.

08. Bitwise AND and Assign (&=)

The "Bitwise AND and Assign" (&=) operator in JavaScript is a compound assignment operator that is used to perform a bitwise AND operation on the current value of a variable and the value on its right-hand side and then assign the result back to that variable. It effectively combines the bitwise AND operation and assignment in a single operation.

let x = 5; // Binary representation: 0101
x &= 3; // Binary representation: 0011

console.log(x); // Outputs: 1 (Binary representation: 0001)

In this example, we have a variable x initially set to 5 (binary 0101). When we use the &= operator, it performs a bitwise AND operation with the value 3 (binary 0011). The result is 1 (binary 0001), which is stored back in the variable x.

09. Bitwise OR and Assign (|=)

The "Bitwise OR and Assign" (|=) operator in JavaScript is a compound assignment operator that is used to perform a bitwise OR operation on the current value of a variable and the value on its right-hand side, and then assign the result back to that variable. It effectively combines the bitwise OR operation and assignment in a single operation.

let x = 5; // Binary representation: 0101
x |= 3; // Binary representation: 0011

console.log(x); // Outputs: 7 (Binary representation: 0111)

In this example, we have a variable x initially set to 5 (binary 0101). When we use the |= operator, it performs a bitwise OR operation with the value 3 (binary 0011). The result is 7 (binary 0111), which is stored back in the variable x.

10. Bitwise XOR and Assign (^=)

The "Bitwise XOR and Assign" (^=) operator in JavaScript is a compound assignment operator that is used to perform a bitwise XOR (exclusive OR) operation on the current value of a variable and the value on its right-hand side, and then assign the result back to that variable. It effectively combines the bitwise XOR operation and assignment in a single operation.

let x = 5; // Binary representation: 0101
x ^= 3; // Binary representation: 0011

console.log(x); // Outputs: 6 (Binary representation: 0110)

In this example, we have a variable x initially set to 5 (binary 0101). When we use the ^= operator, it performs a bitwise XOR operation with the value 3 (binary 0011). The result is 6 (binary 0110), which is stored back in the variable x.

11. Left Shift and Assign (<<=)

The "Left Shift and Assign" (<<=) operator in JavaScript is a compound assignment operator that is used to shift the bits of the current value of a variable to the left by a specified number of positions and then assign the result back to that variable. It effectively combines the left shift operation and assignment in a single operation.

let x = 5; // Binary representation: 0101
x <<= 2; // Binary representation after left shift: 10100

console.log(x); // Outputs: 20 (Binary representation: 10100)

In this example, we have a variable x initially set to 5 (binary 0101). When we use the <<= operator, it shifts the bits of the current value of x two positions to the left. The result is 20 (binary 10100), which is stored back in the variable x.

12. Right Shift and Assign (>>=)

The "Right Shift and Assign" (>>=) operator in JavaScript is a compound assignment operator that is used to shift the bits of the current value of a variable to the right by a specified number of positions and then assign the result back to that variable. It effectively combines the right shift operation and assignment in a single operation.

let x = 16; // Binary representation: 10000
x >>= 2; // Binary representation after right shift: 00100

console.log(x); // Outputs: 4 (Binary representation: 00100)

In this example, we have a variable x initially set to 16 (binary 10000). When we use the >>= operator, it shifts the bits of the current value of x two positions to the right. The result is 4 (binary 00100), which is stored back in the variable x.

13. Unsigned Right Shift and Assign (>>>=)

The "Unsigned Right Shift and Assign" (>>>=) operator in JavaScript is a compound assignment operator that is used to shift the bits of the current value of a variable to the right by a specified number of positions in an unsigned manner (ignoring the sign bit), and then assign the result back to that variable. It effectively combines the unsigned right shift operation and assignment in a single operation.

let x = -10; // Binary representation: 11111111111111111111111111110110
x >>>= 2; // Binary representation after unsigned right shift: 00111111111111111111111111111101

console.log(x); // Outputs: 1073741821 (Binary representation: 00111111111111111111111111111101)

In this example, we have a variable x initially set to -10 (binary 11111111111111111111111111110110). When we use the >>>= operator, it shifts the bits of the current value of x two positions to the right in an unsigned manner. The result is 1073741821 (binary 00111111111111111111111111111101), which is stored back in the variable x.

Bitwise Operators:

Bitwise operators in JavaScript manipulate individual bits of integers, including & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise NOT), << (left shift), and >> (right shift), offering low-level bit-level control.

01. Bitwise AND (&)

The "Bitwise AND" (&) operator in JavaScript is a binary operator that performs a bitwise AND operation on the individual bits of two numbers. It returns a new number where each bit is set to 1 only if the corresponding bits in both numbers are 1.

Here's how the & operator works

  • If a bit is 1 in both operands, the corresponding bit in the result is set to 1.
  • If a bit is 0 in either operand, the corresponding bit in the result is set to 0.
let a = 5; // Binary representation: 0101
let b = 3; // Binary representation: 0011
let result = a & b;

console.log(result); // Outputs: 1 (Binary representation: 0001)

In this example, the & operator performs a bitwise AND operation on the binary representations of a and b. It sets the result to 1 because the first, second, and third bits are 0 in either a or b.

Common Use Cases

  • Masking: Bitwise AND is often used to apply masks to specific bits within a binary representation. For example, we can use it to clear or set certain bits in a number to control flags or settings.
  • Checking Bitwise Flags: In scenarios where you have flags represented as binary bits, we can use bitwise AND to check if specific flags are set or cleared.
  • Low-Level Bit Manipulation: Bitwise AND is used in low-level programming tasks such as hardware control, protocol handling, and binary data manipulation.

02. Bitwise OR (|)

The "Bitwise OR" (|) operator in JavaScript is a binary operator that performs a bitwise OR operation on the individual bits of two numbers. It returns a new number where each bit is set to 1 if the corresponding bit in either or both operands is 1.

Here's how the | operator works

  • If a bit is 1 in either or both operands, the corresponding bit in the result is set to 1.
  • If a bit is 0 in both operands, the corresponding bit in the result is set to 0.
let a = 5; // Binary representation: 0101
let b = 3; // Binary representation: 0011
let result = a | b;

console.log(result); // Outputs: 7 (Binary representation: 0111)

In this example, the | operator performs a bitwise OR operation on the binary representations of a and b. It sets the result to 7 because the first, second, and third bits are 1 in either a or b.

Common Use Cases

  • Combining Bit Flags: Bitwise OR is often used to combine or set specific bits within a binary representation, typically for flag or option settings.
  • Performing Bitwise Operations: In low-level programming and binary data manipulation, bitwise OR is used to perform various operations on individual bits.
  • Creating Bitmasks: Bitwise OR is used to create bitmasks, which are patterns of bits used to extract or manipulate specific information within binary data.

03. Bitwise XOR (^)

The "Bitwise XOR" (^) operator in JavaScript is a binary operator that performs a bitwise XOR (exclusive OR) operation on the individual bits of two numbers. It returns a new number where each bit is set to 1 if the corresponding bits in the operands are different (one is 1, and the other is 0).

Here's how the ^ operator works

  • If a bit is 1 in one operand and 0 in the other, the corresponding bit in the result is set to 1.
  • If both bits are the same (either both 0 or both 1), the corresponding bit in the result is set to 0.
let a = 5; // Binary representation: 0101
let b = 3; // Binary representation: 0011
let result = a ^ b;

console.log(result); // Outputs: 6 (Binary representation: 0110)

In this example, the ^ operator performs a bitwise XOR operation on the binary representations of a and b. It sets the result to 6 because the first and third bits are different (one is 0, and the other is 1).

Common Use Cases

  • Toggling Bits: Bitwise XOR is often used to toggle or invert specific bits within a binary representation.
  • Comparing Flags: In scenarios where flags are represented as binary bits, bitwise XOR is used to compare flags and check if they differ.
  • Data Encryption: Bitwise XOR is employed in some encryption algorithms and error-checking mechanisms.

04. Bitwise NOT (~)

The "Bitwise NOT" (~) operator in JavaScript is a unary operator that performs a bitwise NOT operation on the individual bits of a number. It flips the bits, changing each 0 to 1 and each 1 to 0. The result is a new number with the binary representation inverted.

Here's how the ~ operator works

  • If a bit is 1, it becomes 0 in the result.
  • If a bit is 0, it becomes 1 in the result.
let a = 5; // Binary representation: 00000000000000000000000000000101
let result = ~a;

console.log(result); // Outputs: -6 (Binary representation: 11111111111111111111111111111010)

In this example, the ~ operator performs a bitwise NOT operation on the binary representation of a. It inverts all the bits, changing 00000000000000000000000000000101 to 11111111111111111111111111111010. The result is -6 in decimal.

Common Use Cases

  • Flipping Bits: Bitwise NOT is often used to flip or invert the bits within a number.
  • Creating Bitmasks: It can be used to create bitmasks for specific operations, such as masking or unmasking particular bits.
  • Data Encoding: In some data encoding and decoding algorithms, bitwise NOT is used to reverse the order of bits for transmission or storage.

05. Left Shift (<<)

The "Left Shift" (<<) operator in JavaScript is a binary operator that performs a bitwise left shift operation on the individual bits of a number. It shifts the bits to the left by a specified number of positions and fills the vacant positions with zeros. It effectively multiplies the number by 2 raised to the power of the shift count.

Here's how the << operator works

  • It moves each bit in the binary representation of the number to the left by the specified number of positions.
  • Vacant positions on the right are filled with zeros.
  • The result is a new number.
let a = 5; // Binary representation: 00000000000000000000000000000101
let result = a << 2;

console.log(result); // Outputs: 20 (Binary representation: 00000000000000000000000000010100)

In this example, the << operator performs a left shift operation on the binary representation of a, moving all the bits two positions to the left and filling the vacant positions with zeros. The result is 20 in decimal.

Common Use Cases

  • Multiplying by Powers of 2: Left shifting is often used to multiply a number by 2 raised to a certain power. For example, shifting left by 1 is equivalent to multiplying by 2, and shifting left by 2 is equivalent to multiplying by 4.
  • Bitwise Manipulation: Left shift is used in various bitwise operations and bit manipulation tasks.
  • Creating Bitmasks: It can be used to create bitmasks for specific operations, especially when working with binary flags or settings.

06. Right Shift (>>)

The "Right Shift" (>>) operator in JavaScript is a binary operator that performs a bitwise right shift operation on the individual bits of a number. It shifts the bits to the right by a specified number of positions, effectively dividing the number by 2 raised to the power of the shift count. When shifting right, the vacant positions are filled based on the sign bit (the leftmost bit).

Here's how the >> operator works

  • It moves each bit in the binary representation of the number to the right by the specified number of positions.
  • Vacant positions on the left are filled with the sign bit (0 for positive numbers and 1 for negative numbers).
  • The result is a new number.
let a = 16; // Binary representation: 00000000000000000000000000010000
let result = a >> 2;

console.log(result); // Outputs: 4 (Binary representation: 00000000000000000000000000000004)

In this example, the >> operator performs a right shift operation on the binary representation of a, moving all the bits two positions to the right. The vacant positions on the left are filled with zeros. The result is 4 in decimal.

Common Use Cases

  • Dividing by Powers of 2: Right shifting is often used to divide a number by 2 raised to a certain power. For example, shifting right by 1 is equivalent to dividing by 2, and shifting right by 2 is equivalent to dividing by 4.
  • Handling Arithmetic Operations: Right shift can be used to optimize arithmetic operations on binary representations of numbers.
  • Sign Extension: In signed integer representations, right shift is used for sign extension, preserving the sign bit while shifting.

07. Unsigned Right Shift (>>>)

The "Unsigned Right Shift" (>>>) operator in JavaScript is a binary operator that performs a bitwise right shift operation on the individual bits of a number. It shifts the bits to the right by a specified number of positions, and it always fills the vacant positions on the left with zeros, regardless of the sign bit.

Here's how the >>> operator works

  • It moves each bit in the binary representation of the number to the right by the specified number of positions.
  • Vacant positions on the left are always filled with zeros.
  • The result is a new number.
let a = -16; // Binary representation: 11111111111111111111111111110000
let result = a >>> 2;

console.log(result); // Outputs: 1073741820 (Binary representation: 00111111111111111111111111111100)

In this example, the >>> operator performs an unsigned right shift operation on the binary representation of a, moving all the bits two positions to the right. The vacant positions on the left are filled with zeros, regardless of the sign bit. The result is 1073741820 in decimal.

Common Use Cases

  • Logical Right Shift: The >>> operator is commonly used when a logical right shift is required, and the sign bit should not affect the shifting process.
  • Working with Unsigned Integers: It is useful for handling unsigned integers and binary data where the sign bit is not relevant.
  • Data Processing: In scenarios where data processing involves extracting or manipulating specific bit patterns, the >>> operator ensures that no sign bit is propagated during the shift.

Ternary Operator:

01. Conditional (Ternary) Operator (?:)

The "Conditional (Ternary) Operator" in JavaScript, denoted as condition ? expr1 : expr2, is a shorthand way of writing an if-else statement. It allows us to make decisions and choose between two values or expressions based on the evaluation of a condition.

Here's how the ternary operator works

  • The condition (the part before the ?) is evaluated. If the condition is true, expr1 is executed; if the condition is false, expr2 is executed.
  • If expr1 is executed, its value becomes the result of the entire expression. If expr2 is executed, its value becomes the result.
let age = 25;
let message = age >= 18 ? "You are an adult" : "You are a minor";

console.log(message); // Outputs: 'You are an adult'

In this example, the ternary operator checks if the age is greater than or equal to 18. If the condition is true, it assigns the message 'You are an adult' to the message variable; otherwise, it assigns 'You are a minor'.

Common Use Cases

  • Conditional Assignments: The ternary operator is often used to conditionally assign a value to a variable or property based on a condition.
  • Inline Conditionals: It's useful when you need to make concise, one-liner decisions in your code, such as for formatting or simple calculations.
  • Returning Values: In functions, the ternary operator is employed to return different values or expressions based on certain conditions.

Conclusion

Ultimately, JavaScript operators are the fundamental building blocks that enable developers to perform a wide variety of operations in their code. From basic arithmetic operations to complex logical and bitwise manipulations, operators provide the tools necessary to achieve the desired functionality and logic.

It is important to understand the nuances and behavior of each operator to write efficient and error-free code. To ensure that expressions are evaluated correctly, it is necessary to consider operator precedence and associativity. Furthermore, knowing when and how to use conditional and assignment operators can greatly increase code readability and conciseness.