●   Operators are used to perform operations on values and variables.


Before learning about Operators, It’s important to understand how Quotes work in JS.

We have three types of quotes to write strings:

• " " (Double quotation) - For String
• ' ' (Single quotation) - For String
• ` ` (Backticks) - For String and JSX

Let’s say you want to print this kind of text in console.


        Hello my name is "Urvish" 
    

You might try this


        console.log("Hello my name is "Urvish""); // ❌ Uncaught SyntaxError
    

❌But it doesn’t work — because the inner double quotes confuse JavaScript.
It thinks the string ends early.


✔️We can fix it in two ways:

1. Use backticks or single quotes outside:

We can wrap whole string in to another quotation instead of that one which exist in string.

        console.log(`Hello my name is "Urvish"`); // ✅ Wrap Whole in ` `
        console.log('Hello my name is "Urvish"'); // ✅ Wrap whole in ' '
    

2. Use backslash \ to escape double quotes:

Backslash Helps to consider " " as a string in js.

        console.log("Hello my name is \"Urvish\""); // ✅
    

◈ Arithmetic Operators

Used to perform mathematical calculations:

We can Craft & Calculate any kind of formula using this operators.


        let a = 10, b = 3;

        let sum = a + b;
        console.log("Sum: ", sum);  // 13

        let sub = a - b;
        console.log("Sub: ", sub);  // 7

        let mul = a * b;
        console.log("Mul: ", mul);  // 30

        let div = a / b;
        console.log("Div: ", div );  // 3.333...

        let mod = a % b;
        console.log("Mod: ", mod);  // 1 (remainder)

        let pwr = a ** b;
        console.log("Pwr: ", pwr); // 1000 (a to the power b)

        a++; // Increment
        console.log("Increment: ",a); // 11

        b--; // Decrement
        console.log("Decrement: ",b); // 2
    

        Sum:  13
        Sub:  7
        Mul:  30
        Div:  3.3333333333333335
        Mod:  1
        Pwr:  1000
        Increment:  11
        Decrement:  2
    

◈ Assignment Operators

Used to assign and update values:

This is the simple and short way to assign and update value in itself without using another variable.


        let x = 5;
        console.log("x is " , x);

        x += 3;
        console.log( "x += 3 :", x ); // x = x + 3 → 8

        x -= 2;
        console.log( "x -= 2 :", x ); // x = x - 2 → 6

        x *= 2;
        console.log( "x *= 2 :", x ); // x = x * 2 → 12

        x /= 4;
        console.log( "x /= 4 :", x ); // x = x / 4 → 3

        x %= 2;
        console.log( "x %= 2 :", x ); // x = x % 2 → 1
        
        console.log("x is ", x); // 1
    

        x is  5
        x += 3 : 8
        x -= 2 : 6
        x *= 2 : 12
        x /= 4 : 3
        x %= 2 : 1
        x is  1
    

◈ Comparison Operators

Used to compare values:

Also this can be Used as a conditional Statements in Logical Decisions.


        console.log( 5 == "5" );    // true (values match)
        console.log( 5 === "5" );   // false (types differ)
        console.log( 5 != "5" ) ;    // false
        console.log( 5 !== "5" );   // true
        console.log( 7 > 5 );       // true
        console.log( 7 < 5 );       // false
        console.log( 5 >= 5 );      // true
        console.log( 4 <= 3 );      // false
    

        true   // 5 == "5"
        false  // 5 === "5"
        false  // 5 != "5"
        true   // 5 !== "5"
        true   // 7 > 5 
        false  // 7 < 5 
        true   // 5 >= 5 
        false  // 4 <= 3 
    
Example of Logical Decisions by Conditional Statements.

        // The condition is 10 > 5 
        if(10 > 5){ 
            console.log("Ten is greater than five");
        }
        
        // The condition is 5 > 10
        if(5 > 10){
            console.log("5 is more than 10."); // if condition is True
        }else{
            console.log("5 is not more than 10."); // if condition is false
        }
    

        Ten is greater than five
        5 is not more than 10.
    

◈ Logical Operators

Used with booleans or conditions:


        console.log(true);     // true
        console.log(false);     // false

        console.log(true && true); // true (AND)
        console.log(true && false); // false (AND)
        console.log(false && false); // false (AND)

        console.log(true || true); // true  (OR)
        console.log(true || false); // true  (OR)
        console.log(false || false); // false  (OR)

        console.log(!true);     // false (NOT)
        console.log(!false);     // true (NOT)
    

        true
        false

        true  // true and true = true
        false // true and false = false
        false // false and false = false

        true  // true or true = true
        true  // true or false = true
        false // false or false = false

        false  // Not true
        true   // Not false
    

◈ Ternary Operator

Shorthand for if...else:

Used for deciding Statement from condition too quickly.


        let age = 20;

        let result = (age >= 18) ? "Adult" : "Minor";
        console.log(result); // "Adult"
    

        Adult
    

◈ Bitwise Operators

This Operators are used for Binary operations.

Let's consider 5 and 1.
• 5 = 0101 (4-bit in binary)
• 1 = 0001


        console.log(5 & 1);  // 1 (binary AND)
        console.log(5 | 1);  // 5 (binary OR)
        console.log(5 ^ 1);  // 4 (XOR)
        console.log(~5);     // -6 (NOT)
        console.log(5 << 1); // 10 (shift left)
        console.log(5 >> 1); // 2  (shift right)
    

5 & 1 → Bitwise AND

Rule: Only 1 & 1 gives 1, everything else is 0.


           0101   (5)
        &  0001   (1)
        =  0001   → 1
    
Output

        1
    

5 | 1 → Bitwise OR

Rule: If either bit is 1, result is 1.


           0101   (5)
        |  0001   (1)
        =  0101   → 5
    
Output

        5
    

5 ^ 1 → Bitwise XOR (Exclusive OR)

Rule: Result is 1 only if bits are different.


           0101   (5)
        ^  0001   (1)
        =  0100   → 4
    
Output

        4
    

~5 → Bitwise NOT

Rule: Flips every bit: 1 becomes 0, and 0 becomes 1.
For 32-bit integers:


       00000000 00000000 00000000 00000101   (5)
    ~  11111111 11111111 11111111 11111010   (-6)
    
Output

        -6
    
Why negative? JS uses two's complement system for signed numbers.

5 << 1 → Left Shift

Rule: Shifts bits to the left by 1, adds a 0 at the end (×2 each shift).


           0101   (5)
        << 1
         = 1010   → 10
    
Output

        10
    

5 >> 1 → Right Shift

Rule: Shifts bits to the right by 1 (÷2 each shift).


           0101   (5)
        >> 1
         = 0010   → 2
    
Output

        2
    

Best Practice:

In some case we used + and , in console.log(); or any other way to print output.
This can be little mistake if you can't use properly.


        console.log("Sum: " + 5 + 6);
    
We think output will be Sum: 11, right?
No It will give you wrong answer.

        Sum: 56
    

Due to JavaScript's automatic type conversion,
It start adding into String because we do Concatinating using +.
Also we can get many other bugs if we dont take care of it.

To avoid such this kind of Bugs and Errors. prefer to use , (Semicolon).


        console.log("Sum: " , 5 + 6);
    

        Sum: 11
    

© JurneyToJs by Urvish Patel.