Basic Operators in JavaScript

Basic Operators in JavaScript

A deep dive into how operators work in JavaScript

·

4 min read

JavaScript, like most programming languages, includes a variety of operators that perform operations on data values. Here are some of the basic operators you'll frequently encounter in JavaScript:

  1. Arithmetic Operators: Arithmetic operators are used to perform basic mathematical operations. Here are the most used arithmetic operators in JavaScript:
  1. Addition (+): Adds two operands.

     let a = 5;
     let b = 3;
     let sum = a + b;
     console.log('Addition:', sum); //Output: 8
    
  2. Subtraction (-): Subtracts the right operand from the left operand.

     let a = 5;
     let b = 3;
     let subtraction = a - b;
     console.log('Subtraction:', subtraction); //Output: 2
    
  3. Multiplication (*): Multiplies two operands.

     let a = 10;
     let b = 2;
     let multiplication = a * b;
     console.log("Multiplication: ", multiplication) //Output: 20
    
  4. Division (/): Divides the left operand by the right operand.

     let a = 12;
     let b = 6;
     let division = a / b;
     console.log("Division: ", division) //Output: 2
    
  5. Exponentiation (**): Raises one number to the power of another.

     let power = 2 ** 3; 
     console.log('Exponentiation:', power); // 8
    
  6. Increment (++): Increases a number by 1.

     let c = 5;
     c++; // c = c + 1
     console.log('Increment:', c); // 6
    
  7. Decrement (--): Decreases a number by 1.

     let c = 5;
     c--; // c = c - 1
     console.log('Decrement :', c); // 4
    
  1. Assignment Operators: Assignment operators are used to assign values to variables.

    1. Assignment (=): Assigns the right operand to the left operand.

       let ageOfAdam = 28; "It assigns the value 28 
                            to the variable ageOfAdam"
       console.log(ageOfAdam) //Output: 28
      
    2. Addition Assignment (+=): Adds and assigns the value

       let x = 10 + 5;
       console.log(x) //Output: 15
       x = x + 10;
       console.log(x) //Output: 25
      
       "Now Instead of using the long way to add any value 
       to the variable, we can use addition assignment, which uses 
       the same logic."
      
       x += 10;
       console.log(x) //Output: 35
      
    3. Subtraction Assignment (-=): Subtracts and assigns the value to the variable.

       let x = 20 + 5;
       console.log(x) //Output: 25
       x = x - 10;
       console.log(x) //Output: 15
      
       "Now Instead of using the long way to subtract any value 
       from the variable, we can use subtraction assignment, which uses 
       the same logic."
      
       x -= 10;
       console.log(x) //Output: 5
      
    4. Multiplication Assignment (*=): Multiplies and assigns the value to the variable.

       let x = 20 + 5;
       console.log(x) //Output: 25
       x = x * 10;
       console.log(x) //Output: 250
      
       "Now Instead of using the long way to multiply any value 
       with the variable, we can use multiplication assignment, which uses 
       the same logic."
      
       x *= 10;
       console.log(x) //Output: 2500
      
    5. Division Assignment (/=): Divides and assigns the value to the variable.

       let x = 20 + 5;
       console.log(x) //Output: 25
       x = x / 10;
       console.log(x) //Output: 2.5
      
       "Now Instead of using the long way to divide any value, 
       we can use division assignment, which uses 
       the same logic."
      
       x /= 10;
       console.log(x) //Output: 0.25
      
  2. Comparison Operators: Comparison operators are used to compare two values. They return boolean values which are either True or False.

    1. Equal to (==): Checks if two values are equal.

       let ageOfAdam = 25;
       let ageOfJulia = 24;
      
       let isEqual = (ageOfAdam  == ageOfJulia);
       console.log(isEqual); // false
      
    2. Not equal to (!=): Checks if two values are not equal.

       let ageOfAdam = 25;
       let ageOfJulia = 24;
      
       let isNotEqual = (ageOfAdam  != ageOfJulia);
       console.log(isNotEqual); // true
      
    3. Greater than (>): Checks if one value is greater than another.

       let ageOfAdam = 25;
       let ageOfJulia = 24;
      
       let isGreater = (ageOfAdam  > ageOfJulia);
       console.log(isGreater); // true
      
    4. Greater than or equal to (>=): Checks if one value is greater than or equal to another.

       let ageOfJulia = 18;
       let legalAgeToDrive = 18
       let isGreaterOrEqual = (ageOfJulia >= legalAgeToDrive);
       console.log(isGreater); // true
      
    5. Less than (<): Checks if one value is less than another.

       let ageOfAdam = 25;
       let ageOfJulia = 24;
      
       let isLess = (ageOfAdam < ageOfJulia);
       console.log(isLess); // false
      
    6. Less than or equal to (<=): Checks if one value is less than or equal to another.

       let ageOfAdam = 17;
       let legalAgeToDrive = 18;
      
       let isLessOrEqual = (ageOfAdam <= legalAgeToDrive);
       console.log(isLessOrEqual); // true
      

Conclusion:

These basic operators are the building blocks of JavaScript. By understanding how to use them, you'll be able to write more complex and useful code. Happy coding!