JAVASCRIPT

Operators


 

JavaScript provides various operators that allow us to perform different operations on variables and values.

 

1. Arithmetic Operators:

Arithmetic operators are symbols or characters used to perform mathematical operations on operands.

 

  • ‘+’ → addition operator
  • ‘- ‘ → Subtraction operator 
  • ‘*’ → Multiplication Operator  
  • ‘/’  → Division Operator
  • ‘%’ → Modulus Operator 
  • ‘++’ → Increment Operator 
  • ‘- -' → Decrement Operator

 

Example:

// Arithmetic operators
let x = 10;
let y = 4;
let sum = x + y; // Output: 14
let difference = x - y; // Output: 6
let product = x * y; // Output: 40
let quotient = x / y; // Output: 2.5
let remainder = x % y; // Output:  2
x++; // Increment x by 1
y--; // Decrement y by 1

 

2. Assignment Operators

Assignment operators are symbols used to assign values to variables

  • ‘=’ (Assignment) -> Assigns the value on the right to the variable on the left.
  • ‘+=’ (Addition assignment) -> Adds the value on the right to the variable on the left and assigns the result to the variable on the left.
  • ‘-=’ (Subtraction assignment) -> Subtracts the value on the right from the variable on the left and assigns the result to the variable on the left.
  • ‘*=’ (Multiplication assignment) -> Multiplies the variable on the left by the value on the right and assigns the result to the variable on the left.
  • ‘/=’ (Division assignment) -> Divides the variable on the left by the value on the right and assigns the result to the variable on the left.
  • ‘%=’ (Modulus assignment) -> Takes the modulus of the variable on the left with the value on the right and assigns the result to the variable on the left.
     

Example:

// Assignment Operators
var x = 45; // x is assigned the value 45
var y = "Coders"; // y is assigned the string "Coders"
// `+=`
var x = 5;
x += 3; // Output: 2
// `-=`
var x = 7;
x -= 4; // Output: 3
// `*=`
var x = 3;
x *= 2; // Output: 6
// `/=`
let x = 10;
x /= 2; // Output: 5
// `%=`
var x = 10;
x %= 3; // Output: 1

 

3. Comparison Operators

 

Comparison operators are symbols used in programming to compare two values or expressions and determine their relationship. These operators typically return a boolean value (true or false) based on the comparison result.

 

  • `==` (Equality) -> Checks if two values are equal. Performs type coercion if necessary.
  • `===` ( Strict Equal) -> Checks if two values are equal without performing type coercion. Both value and type must match
  • `!=` (Inequality) -> Checks if two values are not equal. Performs type coercion if necessary.
  • `!==` (Strict Inequality) -> Checks if two values are not equal without performing type coercion.
  • `>`  (Greater than) -> Checks if the value on the left is greater than the value on the right.
  • `>=` (Greater than or equal to) -> Checks if the value on the left is greater than or equal to the value on the right.
  • `<` (Less than) -> Checks if the value on the left is less than the value on the right.
  • `<=` (Less than or equal to) -> Checks if the value on the left is less than or equal to the value on the right.
     

Example:

 

// Comparison Operators
var num1 = 5;
var num2 = '5';
var num3 = 19;

// `==`
console.log(num1 == num2); // true
// `===`
console.log(num1 === num2); // false
// `!=` 
console.log(num1 != num2); // false
// `!==`
console.log(num1 !== num2); // true
// `>`
console.log(num1 > num3) // false
// `<`
console.log(num1 < num3); // true
// `>=`
console.log(num1 >= num3); // false
// `<=`
console.log(num1 <= num3); // true

 

4. Logical Operators

 

Logical operators are symbols used in programming to combine or manipulate boolean values. The three primary logical operators are "AND" (&&), "OR" (||), and "NOT" (!).

 

  • && (Logical AND) -> Returns true if both operands are true, otherwise, it returns false.
  • || (Logical OR) -> Returns true if at least one of the operands is true, otherwise, it returns false.
  • ! (Logical NOT) -> Returns the opposite of the operand's boolean value. If the operand is true, ! returns false, and vice versa.

 

Example:

// Logical Operators
var x = true;
var y = false;
// `&&`
console.log(x && y); // false
// `||`
console.log(x || y); // true
// `!`
console.log(!x); // false