Operators in Javascript are special symbols or characters used to perform operations on variables or values.
Javascript supports various operators that allow us to perform operations on variables and values.
- (Subtraction): The `-` operator is used to subtract one value from another.
+ (Addition): The `+` operator is used to add two values together.
/ (Division): The `/` operator divides one value by another.
* (Multiplication): The `*` operator is used to multiply two values.
** (Exponentiation, ES7): The `**` operator is used to raise the first operand to the power of the second operand.
% (Modulus): The `%` operator returns the remainder of the division of one value by another.
let num1 = 7; let num2 = 3; console.log(num1 - num2); // Output: 4 console.log(num1 + num2); // Output: 10 console.log(num1 / num2); // Output: 2.3 console.log(num1 * num2); // Output: 21 console.log(num1 ** num2); // Output: 343 (7^3) console.log(num1 % num2); // Output: 1
= (Assignment): The simple assignment operator `=` assigns the value on the right-hand side to the variable on the left-hand side.
+= (Addition assignment): The addition assignment operator `+=` adds the value on the right-hand side to the variable on the left-hand side.
-= (Subtraction assignment): The subtraction assignment operator `-=` subtracts the value on the right-hand side to the variable on the left-hand side.
*= (Multiplication assignment): The multiplication assignment operator `*=` multiples the value on the right-hand side to the variable on the left-hand side.
/= (Division assignment): The division assignment operator `/=` divides the value on the right-hand side to the variable on the left-hand side.
%= (Modulus assignment): The modulus assignment operator `%=` performs the modulus operation on the variable on the left-hand side with the value on the right-hand side.
let num1 = 7; // (Assignment) num1 += 5 console.log(num1) // Output: 12
Output is 12, because, above syntax `num1 += 5` equivalent to => num1 = num1 + 5; (Addition Assignment)
let num1 = 12 num1 -= 5; console.log(num1) // Output: 7
Output is 7, because, above syntax `num1 -= 5` equivalent to => num1 = num1 - 5; (Subtraction Assignment)
let num1 = 7; num1 *= 5; console.log(num1) // Output: 35
Output is 35, because above syntax `num1 *= 5` is equivalent to => num1 = num1 * 5; (Multiplication Assignment)
== (Equality, coerces types if needed)
=== (Strict equality, checks both value and type)
!= (Inequality, coerces types if needed)
!== (Strict inequality, checks both value and type)
> (Greater than)
< (Less than)
>= (Greater than or equal to)
<= (Less than or equal to)
let num1 = 7; let num2 = "7"; console.log(num1 == num2); // true (coerces types) console.log(num1 === num2); // false (strict equality) console.log(num1 != num2); // false console.log(num1 !== num2); // true console.log(num1 > num2); // false console.log(num1 < num2); // false console.log(num1 >= num2); // true console.log(num1 <= num2); // true
&& (Logical AND): The logical AND operator `&&` returns `true` if both of its operands are `true`, otherwise, it returns `false`.
|| (Logical OR): The logical OR operator `||` returns `true` if at least one of its operands is `true`, otherwise, it returns `false`.
! (Logical NOT): The logical NOT operator `!` is a unary operator that negates the value of its operand. It returns `true` if the operand is `false`, and `false` if an operand is `true`.
let age = 19; let isStudent = true; console.log(age > 18 && isStudent); // true (AND) isStudent = false; console.log(age > 18 || isStudent); // true (OR) isStudent = true; console.log(!isStudent); // false (NOT)