JavaScript Operators
JavaScript Operators:
Introduction
JavaScript Operators are special symbols that perform specific operations on operands (values or variables). JavaScript provides a wide range of operators for various purposes.
Arithmetic Operators
Arithmetic operators are used for performing mathematical operations.
let a = 10;
let b = 5;
// Addition
console.log(a + b); // Output: 15
// Subtraction
console.log(a - b); // Output: 5
// Multiplication
console.log(a * b); // Output: 50
// Division
console.log(a / b); // Output: 2
// Modulus
console.log(a % b); // Output: 0
// Exponentiation
console.log(a ** b); // Output: 100000
Assignment Operators
Assignment operators are used to assign values to variables.
let x = 10;
// Simple assignment
x = 20;
// Addition assignment
x += 5; // x = x + 5
// Subtraction assignment
x -= 3; // x = x - 3
// Multiplication assignment
x *= 2; // x = x * 2
// Division assignment
x /= 4; // x = x / 4
// Modulus assignment
x %= 3; // x = x % 3
Do You Know?
The assignment operator (=) is used to assign a value to a variable. It does not return a value. Other assignment operators combine an operation with assignment, such as +=, -=, *=, /=, %=.
Comparison Operators
Comparison operators are used to compare two values and return a boolean (true or false) value.
let c = 10;
let d = 5;
// Equal to
console.log(c == d); // Output: false
// Not equal to
console.log(c != d); // Output: true
// Greater than
console.log(c > d); // Output: true
// Less than
console.log(c < d); // Output: false
// Greater than or equal to
console.log(c >= d); // Output: true
// Less than or equal to
console.log(c <= d); // Output: false
Important Note
The strict equality operator (===) checks for both value and data type equality. The loose equality operator (==) only checks for value equality and performs type coercion if necessary.
String Operators
String operators are used to manipulate strings.
let str1 = "Hello";
let str2 = "World";
// Concatenation
console.log(str1 + " " + str2); // Output: Hello World
// String repetition
console.log(str1.repeat(3)); // Output: HelloHelloHello
Logical Operators
Logical operators are used to combine multiple conditions or expressions.
let e = true;
let f = false;
// Logical AND
console.log(e && f); // Output: false
// Logical OR
console.log(e || f); // Output: true
// Logical NOT
console.log(!e); // Output: false
Bitwise Operators
Bitwise operators work on the binary representation of numbers.
let g = 10;
let h = 5;
// Bitwise AND
console.log(g & h); // Output: 0
// Bitwise OR
console.log(g | h); // Output: 15
// Bitwise XOR
console.log(g ^ h); // Output: 15
// Bitwise NOT
console.log(~g); // Output: -11
// Left shift
console.log(g << 2); // Output: 40
// Right shift
console.log(h >> 1); // Output: 2
Avoid This
Bitwise operators are rarely used in modern JavaScript development. They are primarily used in low-level operations or specific algorithms where performance is critical.
Ternary Operator
The ternary operator is a shorthand for an if-else statement.
let i = 10;
let result = (i > 5) ? "Greater than 5" : "Less than or equal to 5";
console.log(result); // Output: Greater than 5
Type Operators
Type operators are used to check the data type of a variable.
let j = 10;
// typeof operator
console.log(typeof j); // Output: number
// instanceof operator
console.log(j instanceof Number); // Output: false
Special Operators
JavaScript has a few special operators for specific purposes.
// Comma operator
let k = (10, 20, 30); // k will be assigned 30
console.log(k); // Output: 30
// Nullish coalescing operator (??)
// Returns the first operand if it is not null or undefined, otherwise returns the second operand.
let l = null;
let m = l ?? 10;
console.log(m); // Output: 10
// Optional chaining operator (?.)
// Allows accessing properties or calling methods of an object that may be null or undefined without throwing an error.
let obj = { name: 'John' };
let age = obj?.age ?? 0;
console.log(age); // Output: 0
Summary
- Operators are special symbols used for performing operations on values or variables.
- Arithmetic operators are used for mathematical calculations.
- Assignment operators assign values to variables.
- Comparison operators compare values and return true or false.
- String operators manipulate strings.
- Logical operators combine conditions or expressions.
- Bitwise operators operate on the binary representation of numbers.
- The ternary operator is a shorthand for if-else statements.
- Type operators check the data type of a variable.
- Special operators like comma, nullish coalescing, and optional chaining are used for specific purposes.