Operators JS
Arithmetic operators
These operators are used to do mathematical calculations:
Addition operator (+)
let x = 4; let y = 5; console.log(x + y);Subtraction operator (-)
let x = 4; let y = 5; console.log(x - y);Multiplication operator (*)
let x = 4; let y = 5; console.log(x * y);Division operator (/)
let x = 4; let y = 5; console.log(x / y);Modulus operator (%)
let x = 4; let y = 5; console.log(x % y);Exponentiation operator (**)
let x = 4; let y = 5; console.log(x ** y);
Assignment operators
These operators are used to assign values to variables. They include:
Assignment operator (=)
let x = 5;Addition assignment operator (+=)
let x = 4; x += 5; // x = x + 5Subtraction assignment operator (-=)
let x = 14; x -= 5; // x = x - 5Multiplication assignment operator (*=)
let x = 14; x *= 5; // x = x * 5Division assignment operator (/=)
let x = 15; x /= 5; // x = x / 5Modulus assignment operator (%=)
let x = 15; x %= 5; // x = x % 5
Comparison operators
These operators are used to compare values. They include:
Equal to operator (==)
5 == 5 // true 5 == '5' // true 'hello' == 'Hello' // false // This operator compares two values for equality, and returns true if they are equal, and false otherwise.Not equal to operator (!=)
5 != 3 // true 5 != 5 // false 'hello' != 'Hello' // true // returns true if they are not equal, and false otherwiseStrict equal to operator (===)
5 === 5 // true 5 === '5' // false // This operator compares two values for strict equality and returns true if they are both of the same type and have the same value, and false otherwise.Strict not equal to operator (!==)
5 !== 3 // true 5 !== 5 // false 'hello' !== 'Hello' // true // returns true if they are either not of the same type or not the same value, and false otherwise.Greater than operator (>)
5 > 3 // true 5 > 10 // falseLess than operator (<)
5 < 10 // true 5 < 3 // falseGreater than or equal to operator (>=)
5 >= 5 // true 5 >= 10 // falseLess than or equal to operator (<=)
5 <= 10 // true 5 <= 3 // false
Logical operators
These operators are used to combine or negate boolean values. They include:
Logical AND operator (&&)
let x = 5; let y = 10; if (x > 0 && y > 0) { console.log("Both x and y are positive."); } // Output: "Both x and y are positive."Logical OR operator (||)
let x = 5; let y = 10; if (x > 0 || y > 0) { console.log("At least one of x and y is positive."); } // Output: "At least one of x and y is positive."Logical NOT operator (!)
let x = 5; let y = 10; if (!(x > y)) { console.log("x is not greater than y."); } // Output: "x is not greater than y."