JavaScript Operators

JavaScript has a variety of operators, which can be categorized into several types.Here are the main types of operators in JavaScript, along with examples.

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations.

Addition (+) : Adds two numbers.
                    let sum = 5 + 3; // sum is 8
                    let concat = 'Hello' + ' ' + 'World'; // concat is 'Hello World'
                
Subtraction (-) : Subtracts the second number from the first.
                    let subtraction = 5 - 3; // subtraction is 2
                    let subtraction = 15 - 5; // subtraction is 10
                
Multiplication(*) : Multiply first and second number.
                    let multiplication = 5 * 3; // multiplication is 15
                    let multiplication = 4 * 6; // multiplication is 24
                
Division(/) : Divide two numbers.
                    let division = 5 / 3; // division is 1.6666666667
                    let division = 25 / 5; // division is 5
                
Modulus(%) : Returns remainder of the number.
                    let remainder = 5 % 2; // remainder is 1
                    let remainder = 10 % 2; // remainder is 0  
                

2. Comparison Operators

Comparison operators are used to compare two values and return a boolean value (true, false).

Equal to (==) : Checks if two values are equal.
        let isEqual = 5 == '5'; // isEqual is true
        let isEqualStrict = 5 === '5'; // isEqualStrict is false (strict equality)
                        
Greater than (>) : Checks if the first value is greater than the second
        let isGreater = 10 > 5; // isGreater is true
        let isGreater = 2 > 5; // isGreater is false
                        
Less than (<) : Checks if the first value is less than the second
        let isLesser = 2 < 5; // isLesser is true
        let isLesser = 10 < 5; // isLesser is false
                        
Greater than or Equal to (>=) : Checks if the first value is greater than or equal to the second
        let isGreaterThanOrEqualTo = 10 >= 5; // isGreaterThanOrEqualTo is true
        let isGreaterThanOrEqualTo = 2 >= 5; // isGreaterThanOrEqualTo is false
                        
Less than or equal to (<=) : Checks if the first value is less than or equal to the second
        let isLesserThanOrEqualTo = 2 <= 5; // isLesserThanOrEqualTo is true
        let isLesserThanOrEqualTo = 10 <= 5; // isLesserThanOrEqualTo is false
                        

3. Logical Operators

Logical operators are used to combine two or more conditions.

Logical AND (&&) : Returns true if both conditions are true.
                    if ( true && true ) // true
                    let andResult = (5 > 3) && (10 > 5); // andResult is true 
                    if ( true && false ) // false
                    let andResultFalse = (5 > 3) && (5 > 10); // andResultFalse is false
                    if ( false && true ) // false
                    let andResultFalse = (3 > 5) && (10 > 5); // andResultFalse is false
                
Logical OR (||) : Returns true if at least one condition is true.
                    if ( true || true ) // true
                    let orResult = (5 > 3) || (10 > 5); // orResult is true
                    if ( true || false ) // true
                    let orResult = (5 > 3) || (5 > 10); // orResult is true 
                    if ( false || false ) // false
                    let orResultFalse = (5 < 3) || (15 < 10); // orResultFalse is false 
                
Logical NOT (!) : Returns true if the condition is false.
                    if ( !false ) // true
                    let andResult = (5 != 3); // andResult is true 
                    if ( !true ) // false
                    let andResultFalse = (5 != 5); // andResultFalse is false
                

4. Assignment Operators

Assignment operators are used to assign values to variables.

Assignment (=) : Assigns the right-hand operand to the left-hand operand.
                    let x = 10; // x is 10 
                    let y = 'Hello'; // y is 'Hello'
                
Addition assignment (+=) : Adds the right-hand operand to the left-hand operand and assigns the result to the left-hand operand.
                    let x = 5;
                    x += 3; // x is 8
                    let str = 'Hello';
                    str += ' World'; // str is 'Hello World'
                
Subtraction assignment (-=) : Subtracts the right-hand operand to the left-hand operand and assigns the result to the left-hand operand.
                    let x = 15;
                    x -= 3; // x is 12
                    let y = 6;
                    y -= 10; // y is -4
                
Multiplication assignment (*=) : Multiply the right-hand operand to the left-hand operand and assigns the result to the left-hand operand.
                    let x = 15;
                    x *= 3; // x is 45
                    let y = 6;
                    y *= -10; // y is -60
                
Division assignment (/=) : Divide the right-hand operand to the left-hand operand and assigns the result to the left-hand operand.
                    let x = 15;
                    x /= 3; // x is 5
                    let y = 6;
                    y /= 10; // y is 0.6
                
Modulus assignment (%=) : Adds the right-hand operand to the left-hand operand and assigns the result to the left-hand operand.
                    let x = 15;
                    x %= 3; // x is 0
                    let y = 17;
                    y %= 2; // y is 1
                

5. String Operators

String operators are used to manipulate strings.

Concatenation (+) : Concatenates two strings.
                    let greeting = 'Hello' + ' ' + 'World'; // greeting is 'Hello World' 
                    let numString = 'The number is ' + 42; // numString is 'The number is 42' 
                    let numString = 'The number is ' + 42 + 2; // numString is 'The number is 44'
                    let numString = 42 + 2 + 'The number is '; // numString is 'The number is 44'  
                
String length (.length) : Returns the length of a string.
                    let length = 'Hello'.length; // length is 5
                    let lengthEmpty = ''.length; // lengthEmpty is 0
                

6. Type Operators

Type operators are used to determine the type of a variable or to convert one type to another.

typeof operator : Returns the type of a variable.
                    let typeOfNumber = typeof 42; // typeOfNumber is 'number'
                    let typeOfString = typeof 'Hello'; // typeOfString is 'string'
                
instanceof operator : Checks if an object is an instance of a specific class or constructor.
                    let isInstance = [] instanceof Array; // isInstance is true 
                    let isNotInstance = {} instanceof Array; // isNotInstance is false                    
                

7. Conditional (Ternary) Operator

The conditional operator assigns a value to a variable based on a condition.

Ternary operator (?) : Assigns one of two values to a variable based on a condition.
                    if (condition == true) ? first is true.
                    let result = (5 > 3) ? 'Yes' : 'No'; // result is 'Yes'
                    if (condition == false) ? second is true.
                    let resultFalse = (5 < 3) ? 'Yes' : 'No'; // resultFalse is 'No'