JS Operators šŸŽÆ

beginner
21 min

JS Operators šŸŽÆ

Welcome to our deep dive into JavaScript Operators! In this comprehensive guide, we'll explore various types of operators in JavaScript, learn their use cases, and understand why they are crucial in building robust applications. Let's get started! šŸ“

Arithmetic Operators šŸ’”

Arithmetic operators perform mathematical operations. Here are the most common ones:

  • Addition (+): Adds two operands.
  • Subtraction (-): Subtracts the right operand from the left operand.
  • Multiplication (*): Multiplies two operands.
  • Division (/): Divides the left operand by the right operand.
  • Modulus (%) (also known as the remainder operator): Returns the remainder of the division of the left operand by the right operand.
  • Increment (++): Increases the value of a variable by 1.
  • Decrement (--): Decreases the value of a variable by 1.

Here's a complete example:

javascript
// Addition let a = 5; let b = 3; let sum = a + b; // sum = 8 // Subtraction let difference = a - b; // difference = 2 // Multiplication let product = a * b; // product = 15 // Division let quotient = a / b; // quotient = 1.6666666666666667 // Modulus let remainder = a % b; // remainder = 1 // Increment let counter = 0; counter++; // counter = 1 // Decrement counter--; // counter = 0

šŸ’” Pro Tip: Using the increment (++) or decrement (--) operator before or after a variable can lead to different results. For example:

javascript
let counter = 0; let result1 = ++counter; // result1 = 1, counter = 1 let result2 = counter++; // result2 = 0, counter = 1

Assignment Operators šŸ’”

Assignment operators are used to store values in variables. Here are the most common ones:

  • Equal (=): Assigns the right operand to the left operand.
  • Addition Assignment (+=): Adds the right operand to the left operand and assigns the result to the left operand.
  • Subtraction Assignment (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.
  • Multiplication Assignment (*=): Multiplies the right operand by the left operand and assigns the result to the left operand.
  • Division Assignment (/=): Divides the left operand by the right operand and assigns the result to the left operand.
  • Modulus Assignment (%): Assigns the remainder of the division of the left operand by the right operand to the left operand.

Here's an example:

javascript
// Assignment let a = 5; // Assignment with addition let sum = a += 3; // sum = 8, a = 8 // Assignment with subtraction let difference = a -= 3; // difference = 5, a = 5 // Assignment with multiplication let product = a *= 2; // product = 10, a = 10 // Assignment with division let quotient = a /= 2; // quotient = 5, a = 5 // Assignment with modulus let remainder = a %= 3; // remainder = 2, a = 3
Quick Quiz
Question 1 of 1

What does the ++ operator do?

Comparison Operators šŸ’”

Comparison operators compare two operands and return a boolean value (true or false). Here are the most common ones:

  • Equal (==): Checks if the values of two operands are equal.
  • Strict Equal (===): Checks if the values and types of two operands are equal.
  • Not Equal (!=): Checks if the values of two operands are not equal.
  • 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 (>=): Checks if the left operand is greater than or equal to the right operand.
  • Less Than or Equal (<=): Checks if the left operand is less than or equal to the right operand.

Here's an example:

javascript
// Equal (==) let a = 5; let b = 5; let equal = a == b; // equal = true // Strict Equal (===) let a = 5; let b = "5"; let strictEqual = a === b; // strictEqual = false // Not Equal (!=) let a = 5; let b = 6; let notEqual = a != b; // notEqual = true // Greater Than (>) let a = 5; let b = 6; let greaterThan = a > b; // greaterThan = false // Less Than (<) let a = 5; let b = 6; let lessThan = a < b; // lessThan = true // Greater Than or Equal (>=) let a = 5; let b = 6; let greaterThanOrEqual = a >= b; // greaterThanOrEqual = false // Less Than or Equal (<=) let a = 5; let b = 6; let lessThanOrEqual = a <= b; // lessThanOrEqual = true
Quick Quiz
Question 1 of 1

What does the strict equal operator (===) do?

Logical Operators šŸ’”

Logical operators combine boolean expressions. Here are the most common ones:

  • AND (&&): Returns true if both operands are true.
  • OR (||): Returns true if at least one operand is true.
  • NOT (!): Returns the opposite (true to false or false to true) of the operand.

Here's an example:

javascript
// AND (&&) let a = true; let b = false; let and = a && b; // and = false // OR (||) let a = false; let b = true; let or = a || b; // or = true // NOT (!) let isValid = true; let not = !isValid; // not = false
Quick Quiz
Question 1 of 1

What does the NOT operator (!) do?

That's a wrap on our deep dive into JavaScript operators! Mastering these operators is essential for building powerful applications. Keep practicing, and you'll be a JavaScript wizard in no time! šŸš€

Happy coding! šŸ’»