JavaScript : Operators

In JavaScript, operators are symbols that perform operations on operands, such as variables, values, or expressions. JavaScript supports various types of operators for different purposes, such as arithmetic, comparison, logical operations, assignment, and more. Let's explore some of the commonly used operators:

1. Arithmetic Operators:


Arithmetic operators are used to perform arithmetic operations on numerical values.

  • Addition (+): Adds two operands.


  let sum = 5 + 3;  // Result: 8
 

  • Subtraction (-): Subtracts the second operand from the first.


  let difference = 10 - 5;  // Result: 5
  

  • Multiplication (*): Multiplies two operands.


  let product = 3 * 4;  // Result: 12
 

  • Division (/): Divides the first operand by the second.

  
  let quotient = 12 / 3;  // Result: 4
  

  • Modulus (%): Returns the remainder of the division.


  let remainder = 10 % 3;  // Result: 1
  

2. Comparison Operators:


Comparison operators are used to compare two values and return a Boolean result ( true or false ).

  • Equal to (==): Checks if two operands are equal.


  console.log(5 == 5);  // Result: true
 

  • Not equal to (!=): Checks if two operands are not equal.


  console.log(5 != 3);  // Result: true
 

  • Strict equal to (===): Checks if two operands are equal without type conversion (strict equality).

  
  console.log(5 === "5");  // Result: false
  

  • Strict not equal to (!==): Checks if two operands are not equal without type conversion (strict inequality).


  console.log(5 !== "5");  // Result: true
 

  • Greater than (>): Checks if the left operand is greater than the right operand.
  • Less than (<): Checks if the left operand is less than the right operand.
  • Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
  • Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.

3. Logical Operators:


Logical operators are used to combine or manipulate Boolean values.

  • Logical AND ( && ): Returns true if both operands are true.


  console.log(true && true);   // Result: true
  console.log(true && false);  // Result: false

  

  • Logical OR ( || ): Returns true if at least one of the operands is true.


  console.log(true || false);  // Result: true
  console.log(false || false); // Result: false

 

  • Logical NOT ( ! ): Returns the opposite Boolean value of the operand.


  console.log(!true);   // Result: false
  console.log(!false);  // Result: true

 

4. Assignment Operators:


Assignment operators are used to assign values to variables.

  • Assignment ( = ): Assigns the value of the right operand to the left operand.

 
  let x = 10;
 

  • Addition assignment ( += ): Adds the value of the right operand to the variable and assigns the result to the variable.

  
  let y = 5;
  y += 3;  // Equivalent to: y = y + 3; Result: 8

  

  • Subtraction assignment ( -= )**: Subtracts the value of the right operand from the variable and assigns the result to the variable.
  • Multiplication assignment ( *= )**: Multiplies the variable by the value of the right operand and assigns the result to the variable.
  • Division assignment ( /= )**: Divides the variable by the value of the right operand and assigns the result to the variable.

5. Unary Operators:


Unary operators are used on a single operand.

  • Unary plus ( + ): Converts an operand into a number.


  let num = +"10";  // Result: 10
 

  • Unary minus ( - ): Converts an operand into a negative number.
  • Increment ( ++ ): Increments the value of a variable by 1.
  • Decrement ( -- ): Decrements the value of a variable by 1.

Example Usage:

Here's an example demonstrating the use of different operators in JavaScript:


let a = 10;
let b = 5;

// Arithmetic operators
let sum = a + b;     // Result: 15
let difference = a - b;  // Result: 5
let product = a * b;  // Result: 50
let quotient = a / b;  // Result: 2
let remainder = a % b;  // Result: 0

// Comparison operators
console.log(a > b);   // Result: true
console.log(a === b); // Result: false

// Logical operators
let isTrue = true;
let isFalse = false;
console.log(isTrue && isFalse);  // Result: false
console.log(isTrue || isFalse);  // Result: true
console.log(!isTrue);            // Result: false

// Assignment operators
let c = 20;
c += 5;  // Equivalent to: c = c + 5; Result: 25

// Unary operators
let num = -10;  // Unary minus
console.log(num);  // Result: -10
let count = 0;
count++;  // Increment
console.log(count);  // Result: 1
count--;  // Decrement
console.log(count);  // Result: 0

 

Conclusion:


JavaScript operators are essential for performing various operations in your code, such as calculations, comparisons, logical evaluations, and more. Understanding how to use these operators and their behavior is fundamental for writing functional and efficient JavaScript programs.