JavaScript Operators
Javascript operators are used to perform different types of mathematical and logical computations.
-
Arithmetic Operators
-
Assignment Operators
-
Comparison Operators
-
Logical Operators
-
Ternary (Conditional) Operator
-
Bitwise Operators
Arithmetic Operators
An arithmetic operator accepts numerical values as operands and returns a single numerical value. The numerical values can be literals or variables.
- Addition (+) : Takes two numerical operands and gives their numerical sum. It also concatenates two strings or numbers.
let sum = 10 + 20;console.log(sum); // 30- Subtraction(-) : It gives the difference between two operands in the form of numerical value.
let result = 30 - 10;console.log(result); // 20- Multiplication(*) : Gives the product of operands where one operand is a multiplicand and another is multiplier.
let product = 2 * 3;console.log(product); // 6- Division(/) : provides the quotient of its operands where the right operand is the divisor and the left operand is the dividend.
let divide = 20 / 10;console.log(divide); // 2- Modulus (%) : The modulus operator returns the remainder left over when a dividend is divided by a divisor. The modulus operator is also known as the remainder operator.
let x = 9 % 5;console.log(x); // 4- Exponentiation ( ** ) : The exponentiation operator gives the result of raising the first operand to the power of the second operand.
let y = -(4 ** 2);console.log(y); // -16- Increment (++) : The increment operator increments (adds one to) its operand and returns a value.
// Postfixlet a = 2;let b = a++; // b = 2, a = 3// Prefixlet c = 5;let d = ++c; // c = 6, d = 6- Decrement (—) : The decrement operator decrements (subtracts one from) its operand and returns a value.
// Prefixlet a = 2;b = --a;// Postfixlet c = 3;d = c--;- Unary Negation (-) : This is a unary operator i.e. it operates on a single operand. It gives the negation of an operand.
let i = 3;j = -i; // -3- Unary Plus (+) : This is a way to convert a non-number into a number.
let a = +"2"; // 2let b = +true; // 1let c = +false; // 0let d = +null; // 0Assignment Operators
In JavaScript, an assignment operator (=) assigns a value to a variable.
a = b // a = b Assigns the value of b to a.a += b // a = a + b Assigns the result of a + b to a.a -= b // a = a - b Assigns the result of a - b to a.a *= b // a = a * b Assigns the result of a x b to a.a /= b // a = a / b Assigns the result of a/b to a.a %= b // a = a % b Assigns the remainder of a and b to a.a &=b // a = a & b Assigns the result of a AND b to a.a |=b // a = a | b Assigns the result of a OR b to a.a ^=b // a = a ^ b Assigns the result of a XOR b to a.a <<= b // a = a << b Assigns the result of a shifted left by b to a.a >>= b // a = a >> b Assigns the result of a shifted right (sign preserved) by b to a.a >>>= b // a = a >>> b Assigns the result of a shifted right by b to a.Comparison Operators
To compare two values, you use a comparison operator,
<less than>greater than<=less than or equal to>=greater than or equal to==equal to!=not equal to===strictly equal to!==strictly not equal to
let num1 = 20 > 10; // truelet num2 = 20 < 10; // falselet num3 = 10 == 10; // truelet num4 = 10 === '10'; // falselet num5 = 10 !== '10'; // truelet num6 = 20 >= 20; // truelet num7 = 20 <= 10; // falseLogical Operators
Logical operators in JavaScript are used to perform logical operations on values and return either true or false. These operators are commonly used in decision-making statements like if or while loops to control the flow of execution based on conditions.
&&Logical AND||Logical OR!Logical NOT
let age = 20;let pass = true;
// Logical AND checks both conditionsif (age >= 18 && pass) { console.log("all conditions true");}
if (age >= 21 || pass) { console.log("any one condition true");}
console.log(!pass); // false (if true then false , if false then true)Ternary Operator
The Ternary Operator in JavaScript is a shortcut for writing simple if-else statements. It’s also known as the Conditional Operator because it works based on a condition. The ternary operator allows you to quickly decide between two values depending on whether a condition is true or false.
let age = 22let res = (age > 18) ? "Allow" : "Reject";console.log(res) // Allow