DEV Community

Rakshambika
Rakshambika

Posted on

Operators in JavaScript Part-1

Operators:

  • Operators are special symbols used to perform operations on operands (values or variables).

Types of Operators:

      - Arithmetic Operator
      - Assignment operators
      - Comparison operators
      - Logical operators
      - Bitwise operators
      - Conditional (ternary) operators
      - String operators
Enter fullscreen mode Exit fullscreen mode

Arithmetic Operators:

  • Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, division, and modulus on numeric values.
Operator Description
Addition (+) It returns the value after addition.
Subtraction (-) It returns the value after subtraction.
Multiplication (*) It returns the value after multiplying the operands.
Division (/) It returns the value after dividing 2 operands.
Modulus (%) It returns the integer remainder after dividing 2 operands.
Increment (++) It adds 1 to the operand. For the prefix operator (++var2), it returns the value after adding 1. For the postfix operator (var2++), it adds 1 to the value and then returns the original value.
Decrement (--) It subtracts 1 from the operand. For the prefix operator (--var2), it returns the value after subtracting 1. For the postfix operator (var2--), it subtracts 1 from the value and then returns the original value.
Exponentiation operator (**) It calculates the base to the exponent power, i.e., base ** exponent.

Example:

 // Arithmetic Operator
        let a=100;
        let b=50;
        console.log(`Addition - ${a - b}`);
        console.log(`Subtraction - ${a - b}`);
        console.log(`Multiplication - ${a * b}`);
        console.log(`Division - ${a / b}`);
        console.log(`Modulus - ${a % b}`);
        console.log(`Pre-Increment - ${++a}`);
        console.log(`Post-Increment - ${a++}`);
        console.log(`Value of a - ${a}`);
        console.log(`Pre-Decrement - ${--a}`);
        console.log(`Post-Decrement - ${a--}`);
        console.log(`Value of a - ${a}`);

Enter fullscreen mode Exit fullscreen mode

Output:


Assignment Operator:

  • Assignment operator in JavaScript is used to assign a value to a variable or property.
Name Shorthand Meaning
Assignment x = y x = y
Addition Assignment x += y x = x + y
Subtraction Assignment x -= y x = x - y
Multiplication Assignment x *= y x = x * y
Division Assignment x /= y x = x / y
Remainder Assignment x %= y x = x % y
Exponentiation Assignment x **= y x = x ** y

Example:

//Assignment Operator
        let a=100;
        let b=50;
        let c=2;
        let d=4;
        console.log(`Addition Assignment - ${a += b}`);
        console.log(`Subtraction Assignment - ${a -= b}`);
        console.log(`Multiplication Assignment - ${a *= b}`);
        console.log(`Division Assignment - ${a /= b}`);
        console.log(`Remainder Assignment - ${a %= b}`);
        console.log(`Exponentiation Assignment - ${c **= d}`);

Enter fullscreen mode Exit fullscreen mode

Output:


Comparison operators:

  • Comparison operators are used to compare two values. The result of a comparison is always a Boolean value: true or false.
Operator Description
Equal (==) It returns true if the operands are equal.
Not equal (!=) It returns true if the operands are not equal.
Strict equal (===) It returns true if the operands are equal and of the same type.
Strict not equal (!==) It returns true if the operands are not equal or are of different types.
Greater than (>) It returns true if the left operand is greater than the right operand.
Greater than or equal (>=) It returns true if the left operand is greater than or equal to the right operand.
Less than (<) It returns true if the left operand is less than the right operand.
Less than or equal (<=) It returns true if the left operand is less than or equal to the right operand.

Example:

// Comparison Operators
        let a = 100;
        let b = 50;

        console.log(`Equal - ${a == b}`);
        console.log(`Not Equal - ${a != b}`);
        console.log(`Strict Equal - ${a === b}`);
        console.log(`Strict Not Equal - ${a !== b}`);
        console.log(`Greater Than - ${a > b}`);
        console.log(`Greater Than or Equal - ${a >= b}`);
        console.log(`Less Than - ${a < b}`);
        console.log(`Less Than or Equal - ${a <= b}`);
Enter fullscreen mode Exit fullscreen mode

Output:


Logical Operators:

  • Logical operators are used to combine or reverse conditions. They return a Boolean value (true or false).
Operator Description
Logical AND The AND operator returns true if both expressions are true, otherwise false.
Logical OR The OR operator returns true if one or both expressions are true, otherwise false.
Logical NOT The NOT operator (!) returns true for false expressions and false for true expressions.

Example:

// Logical Operators
let a = 100;
let b = 50;

console.log(`Logical AND - ${a > b && b > 0}`);
console.log(`Logical OR - ${a < b || b > 0}`);
console.log(`Logical NOT - ${!(a > b)}`);
Enter fullscreen mode Exit fullscreen mode

Output:


Top comments (2)

Collapse
 
berlin_07_87ce4b42d1d5c02 profile image
Berlin_07

Very useful

Collapse
 
raksha_murugesan profile image
Rakshambika

Thank You