4 – Operators (Javascript)

Understanding Operators in JavaScript

Operators in JavaScript are essential tools for performing various operations on data, from basic arithmetic calculations to more complex logical comparisons. They allow you to manipulate and process data, making JavaScript a versatile language for a wide range of applications.

Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations. JavaScript provides a set of common arithmetic operators:

  • Addition (+): Used to add two numbers together.
  • Subtraction (-): Used to subtract the right operand from the left.
  • Multiplication (*): Used to multiply two values.
  • Division (/): Used to divide the left operand by the right.
  • Modulus (%): Returns the remainder of the division of two numbers.

Here’s an example of using arithmetic operators:


        let a = 10;
        let b = 5;

        let additionResult = a + b; // 15
        let subtractionResult = a - b; // 5
        let multiplicationResult = a * b; // 50
        let divisionResult = a / b; // 2
        let modulusResult = a % b; // 0
    
Comparison Operators

Comparison operators are used to compare two values and return a Boolean result (true or false). They are often used in conditional statements to make decisions in your code. Some common comparison operators include:

  • Equal (==): Checks if two values are equal.
  • Not equal (!=): Checks if two values are not equal.
  • Greater than (>): Checks if the left value is greater than the right.
  • Less than (<): Checks if the left value is less than the right.
  • Greater than or equal to (>=): Checks if the left value is greater than or equal to the right.
  • Less than or equal to (<=): Checks if the left value is less than or equal to the right.

Example of using comparison operators:


        let x = 5;
        let y = 10;

        let isEqual = x == y; // false
        let isNotEqual = x != y; // true
        let isGreater = x > y; // false
        let isLess = x < y; // true
        let isGreaterOrEqual = x >= y; // false
        let isLessOrEqual = x <= y; // true
    
Logical Operators

Logical operators allow you to combine or manipulate Boolean values. JavaScript provides three main logical operators:

  • AND (&&): Returns true if both operands are true.
  • OR (||): Returns true if at least one operand is true.
  • NOT (!): Returns the opposite of the operand’s truth value.

Example of using logical operators:


        let isTrue = true;
        let isFalse = false;

        let andResult = isTrue && isFalse; // false
        let orResult = isTrue || isFalse; // true
        let notResult = !isTrue; // false
    
Assignment Operators

Assignment operators are used to assign values to variables. The most common assignment operator is the equal sign (=). However, JavaScript provides shorthand assignment operators that combine arithmetic and assignment, such as +=-=*=, and /=.

Example of using assignment operators:


        let num = 5;

        num += 3; // num is now 8
        num -= 2; // num is now 6
        num *= 4; // num is now 24
        num /= 3; // num is now 8
    
Conclusion

Operators in JavaScript are vital for working with data and controlling the flow of your programs. They empower you to perform calculations, make decisions, and manipulate data efficiently. Understanding how to use various operators is a key part of becoming proficient in JavaScript programming.