Operators - potatoscript/javascript GitHub Wiki

🔧 Operators in JavaScript 🛠️

In JavaScript, operators are symbols used to perform operations on variables and values. These operations can be arithmetic, logical, or comparison-based. Operators are essential tools for manipulating data and making decisions in your code.

Let's break down the different types of operators in JavaScript.


🧮 1. Arithmetic Operators ➗

Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, and division.

  • Addition (+): Adds two values together.

    let sum = 5 + 3;  // sum = 8
    
  • Subtraction (-): Subtracts the second value from the first value.

    let difference = 10 - 4;  // difference = 6
    
  • Multiplication (*): Multiplies two values.

    let product = 6 * 2;  // product = 12
    
  • Division (/): Divides the first value by the second value.

    let quotient = 12 / 4;  // quotient = 3
    
  • Modulus (%): Returns the remainder of a division.

    let remainder = 7 % 3;  // remainder = 1
    
  • Exponentiation (**): Raises the first number to the power of the second number.

    let power = 2 ** 3;  // power = 8
    
  • Increment (++): Increases a variable by 1.

    let counter = 5;
    counter++;  // counter = 6
    
  • Decrement (--): Decreases a variable by 1.

    let counter = 5;
    counter--;  // counter = 4
    

🔍 2. Comparison Operators ⚖️

Comparison operators are used to compare two values. They return a Boolean value (true or false) based on the comparison.

  • Equal to (==): Checks if two values are equal, but does not consider data type.

    let isEqual = 5 == '5';  // isEqual = true (type coercion happens)
    
  • Strict Equal to (===): Checks if two values are equal and of the same type.

    let isStrictEqual = 5 === '5';  // isStrictEqual = false (no type coercion)
    
  • Not Equal to (!=): Checks if two values are not equal.

    let isNotEqual = 10 != 15;  // isNotEqual = true
    
  • Strict Not Equal to (!==): Checks if two values are not equal or not of the same type.

    let isStrictNotEqual = 10 !== '10';  // isStrictNotEqual = true
    
  • Greater Than (>): Checks if the first value is greater than the second.

    let isGreaterThan = 7 > 4;  // isGreaterThan = true
    
  • Less Than (<): Checks if the first value is less than the second.

    let isLessThan = 3 < 5;  // isLessThan = true
    
  • Greater Than or Equal to (>=): Checks if the first value is greater than or equal to the second.

    let isGreaterOrEqual = 6 >= 6;  // isGreaterOrEqual = true
    
  • Less Than or Equal to (<=): Checks if the first value is less than or equal to the second.

    let isLessOrEqual = 4 <= 10;  // isLessOrEqual = true
    

💭 3. Logical Operators ⛔

Logical operators are used to combine multiple conditions and return true or false based on those conditions.

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

    let isAdult = true;
    let hasTicket = false;
    let canEnter = isAdult && hasTicket;  // canEnter = false
    
  • Logical OR (||): Returns true if at least one of the conditions is true.

    let isAdult = true;
    let hasTicket = false;
    let canEnter = isAdult || hasTicket;  // canEnter = true
    
  • Logical NOT (!): Reverses the result of a condition (turns true to false and vice versa).

    let isRainy = false;
    let isNotRainy = !isRainy;  // isNotRainy = true
    

🔢 4. Assignment Operators 🖋️

Assignment operators are used to assign values to variables. These operators simplify common assignments.

  • Assignment (=): Assigns the value of the right-hand side to the left-hand side.

    let x = 10;  // x is assigned the value of 10
    
  • Add and Assign (+=): Adds the right-hand side value to the left-hand side variable and assigns the result.

    let x = 5;
    x += 3;  // x = 8
    
  • Subtract and Assign (-=): Subtracts the right-hand side value from the left-hand side variable and assigns the result.

    let x = 5;
    x -= 2;  // x = 3
    
  • Multiply and Assign (*=): Multiplies the left-hand side variable by the right-hand side value and assigns the result.

    let x = 5;
    x *= 2;  // x = 10
    
  • Divide and Assign (/=): Divides the left-hand side variable by the right-hand side value and assigns the result.

    let x = 10;
    x /= 2;  // x = 5
    
  • Modulus and Assign (%=): Calculates the modulus (remainder) of the division and assigns the result.

    let x = 10;
    x %= 3;  // x = 1 (remainder of 10 / 3)
    

🔢 5. Ternary Operator 🎯

The ternary operator is a shorthand way of writing an if...else statement. It has the form:

condition ? expressionIfTrue : expressionIfFalse;
  • Example:
    let age = 18;
    let result = age >= 18 ? "Adult" : "Minor";  // result = "Adult"
    

📝 6. Typeof Operator 🔍

The typeof operator is used to determine the type of a variable or value. It returns a string indicating the type.

  • Example:
    let name = "Lucy";
    console.log(typeof name);  // "string"
    
    let age = 25;
    console.log(typeof age);   // "number"
    
    let isActive = true;
    console.log(typeof isActive);  // "boolean"