Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

Updated
3 min read

In JavaScript, operators are symbols that perform operations on value & varialbes. JavaScript uses various operators to perform mathematical, logical, and comparison tasks on values and variables.

Here are the core operator types that will be covered in this blog

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Let's go first with Arithmetic Operators Arithmetic Operators are used to perform mathematical calculations,

  • `+`: Addition
  • `-`: Subtraction
  • `*`: Multiplication
  • `/`: Division
  • `%`: Remainder (Modulo)
  • Here is an example of Arithmetic Operators

    let a = 30; 
    let b = 20;
    
    console.log(a + b); // 50 
    console.log(a - b); // 10
    console.log(a * b); // 600
    console.log(a / b); // 1.5
    console.log(a % b); // 10 ,it returns the remainder after division
    

    Assignment Operators Assignment Operators are used to assign values to variables. for example:

  • `=`: Simple Assignment
  • `+=`: Addition Assignment
  • `-=`: Subtraction Assignment
  • `*=`: Multiplication Assignment
  • `/=`: Division Assignment
  • `%=`: Remainder Assignment
  • in code

        let a = 10 // 10
        a += 5 // 15
        a -= 5 // 10
        a *= 2 // 20
        a /= 2 // 10
        a %= 5 // 0
    

    ** Comparison Operators ** Comparison Operators are used to compare two values in JavaScript, and return a Boolean value true or false depending upon the condition. Comparison Operators are mostly used in logical statements to determine equality.

    Here are some Comparison Operators

  • `>`: Greater than, return `true` if left value is greater than right
  • `<`: Less Than, returns `true` if the left value is less than the right.
  • `>=`: Greater Than or Equal, returns `true` if the left value is greater than or equal to the right.
  • `<=`: Less Than or Equal, returns `true` if the left value is less than or equal to the right.
  • `==`: Equailty, returns `true` if values are equal.
  • `===`: Strict Equality, returns `true` if values & types are equal.
  • `!=`: Inequailty, returns `true` if values are not equal.
  • `!==`: Strict Inequality, returns true if values & types are not equal
  • in code

        let price = 30
        price > 40 // false
        price < 40 // true
        price >= 40 // false
        price <= 40 // true
        price == 40 // false
        price === 40 // false
        price != 40 // true
        price !== 40 // true
    

    A small doubt you may having that is what is the difference between == & ===, well, == compares only values (with type coercion), while === compares both value and type (no coercion).

    let str = '1'
    let num = 1
    str == num // true
    str === num // false
    

    Logical Operators

    Logical Operators are used to combine conditional statements, and return a boolean value true or false depending upon the condition. Logical Operators also help in creating complex if statements.

    Here are some logical operators

  • `&&`: AND, returns `true` if both operands are true.
  • `||`: OR, returns `true` if at least one operand is true.
  • `!`: NOT, returns `true` if the operand is false.
  • in code

     let price = 50
     let discount = 10
    
     price > 40 && discount > 5 // true 
     price > 40 || discount <= 10 // true
     
     let isLoggedIn = false 
     !isLoggedIn // true
    

    Hope you learn something new from this blog,