Mastering Operators in JavaScript πŸš€ | JavaScript Journey | In Hindi | Death Code - DeathCode

JavaScript Operators Guide

JavaScript provides a variety of operators that allow you to perform operations on variables and values. This guide covers the main types of operators in JavaScript.

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations.

  • + : Addition
  • - : Subtraction
  • * : Multiplication
  • / : Division
  • % : Modulus (remainder)
  • ++ : Increment
  • -- : Decrement
  • ** : Exponentiation

Example Code

     // Arithmetic Operators (+,-,*,/,%,++,--,**)  
     let rem = 47 % 3; // Remainder of 47 divided by 3 
     let num = 4;  // 
     console.log(num++); // Outputs: 4 (num is incremented after this line) 
     console.log(num); // Outputs: 5 (num is now 5 after increment) 
     console.log(5 ** 4); // Outputs: 625 (5 raised to the power of 4)  // 
     console.log(rem); // Outputs: 2 (the remainder)

Explanation

  • rem calculates the remainder of 47 divided by 3, which is 2.
  • num++ increments num by 1 but returns the original value (4) before the increment.
  • 5 ** 4 calculates 5 raised to the power of 4, resulting in 625.

2. Assignment Operators

Assignment operators are used to assign values to variables.

  • = : Assigns a value
  • += : Adds and assigns
  • -= : Subtracts and assigns
  • *= : Multiplies and assigns
  • /= : Divides and assigns

Example Code

     // Assignment Operators (=,+=,-=,*=,/=)  
     let bi = 32; 
     bi -= 16; // bi = bi - 16, bi is now 16 
     bi *= 2;  // bi = bi * 2, bi is now 32 
     bi /= 2;  // bi = bi / 2, bi is now 16 
     let a = b = c = 66; // Assigns 66 to a, b, and c  // 
     console.log([a, b, c]); // Outputs: [66, 66, 66]

Explanation

  • bi is initially set to 32. The subsequent operations modify its value through subtraction, multiplication, and division.
  • The line let a = b = c = 66; assigns the value 66 to variables a, b, and c simultaneously.

3. Comparison Operators

Comparison operators are used to compare two values and return a boolean result.

  • == : Equal to
  • === : Strict equal to (checks value and type)
  • != : Not equal to
  • !== : Strict not equal to
  • < : Less than
  • > : Greater than
  • <= : Less than or equal to
  • >= : Greater than or equal to

Example Code

     // Comparison Operators (==,<,>,<=,>=,===,!==,!=)  // 
     console.log(2 == 2); // true // 
     console.log(2 == 4); // false // 
     console.log(2 > 2); // false // 
     console.log(2 >= 8); // false // 
     console.log(2 != 2); // false // 
     console.log('2' === 2); // false (strict comparison)  // 
     console.log(null >= 1); // false // 
     console.log(undefined >= 1); // false

Explanation

  • Comparison operators evaluate the relationship between two values, returning true or false.
  • For example, 2 == 2 returns true, while '2' === 2 returns false due to type difference.

4. Logical Operators

Logical operators are used to combine multiple boolean expressions.

  • && : Logical AND
  • || : Logical OR
  • ! : Logical NOT
  • ?? : Nullish coalescing operator

Example Code

     // Logical Operators (&&,||,!,??) // 
     console.log(2 == 2 && 1 == 1); // true // 
     console.log(2 == 4 || 1 == 0); // false // 
     console.log(!(2 < 7)); // false  
     let score = 9; 
     let newScore = score ?? 0; // newScore is 9 since score is not null or undefined 
     console.log(newScore); // Outputs: 9

Explanation

  • Logical operators evaluate boolean expressions and return a boolean result.
  • The nullish coalescing operator ?? returns the right-hand operand when the left-hand operand is null or undefined.

Conclusion

Understanding operators is crucial for effective programming in JavaScript. This guide provides a foundational overview of the various operators available, along with practical examples to illustrate their usage.

Β© 2024 DeathCode. All Rights Reserved.