JavaScript Operators
This are some of the most commonly used JavaScript operators:
You can conduct a number of operations on various kinds of data using JavaScript's extensive set of operators. The most popular JavaScript functions are listed below:
Arithmetic Operators:
These operators are used to perform mathematical operations like addition, subtraction, multiplication, and division. They include + (addition), - (subtraction), * (multiplication), / (division), % (modulus), ++ (increment), and -- (decrement).
Example:
let a = 5; let b = 2; console.log(a + b); // Output: 7
console.log(a - b); // Output: 3
console.log(a * b); // Output: 10
console.log(a / b); // Output: 2.5
console.log(a % b); // Output: 1
console.log(a++); // Output: 5
console.log(++a); // Output: 7
console.log(b--); // Output: 2
console.log(--b); // Output: 0
Assignment Operators:
These operators are used to assign values to variables. They include = (assignment), += (addition assignment), -= (subtraction assignment), *= (multiplication assignment), /= (division assignment), %= (modulus assignment).
Example:
let x = 5; let y = 3; x += y; // Equivalent to x = x + y
console.log(x); // Output: 8 x %= y; // Equivalent to x = x % y
console.log(x); // Output: 2
Comparison Operators:
These operators are used to compare two values and return a Boolean value (true or false). They include == (equality), === (strict equality), != (inequality), !== (strict inequality), > (greater than), < (less than), >= (greater than or equal to), and <= (less than or equal to).
Example:
let a = 5; let b = 2; console.log(a == b); // Output: false
console.log(a === "5"); // Output: false
console.log(a != b); // Output: true
console.log(a !== "5"); // Output: true
console.log(a > b); // Output: true console.log(a < b); // Output: false
console.log(a >= 5); // Output: true console.log(b <= 2); // Output: true
Logical Operators:
These operators are used to perform logical operations on Boolean values. They include && (logical AND), || (logical OR), and ! (logical NOT).
Example:
let a = true; let b = false; console.log(a && b); // Output: false
console.log(a || b); // Output: true console.log(!a); // Output: false
console.log(!b); // Output: true
Bitwise Operators:
These operators are used to perform bitwise operations on integer values. They include & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise NOT), << (left shift), and >> (right shift).
Example:
Let a = 5; // Equivalent to binary 0101 let b = 3; // Equivalent to binary 0011 console.log(a & b); // Output: 1 (Equivalent to binary 0001)
console.log(a | b); // Output: 7 (Equivalent to binary 0111)
console.log(a ^ b); // Output: 6 (Equivalent to binary 0110)
Last updated