Skip to main content

Command Palette

Search for a command to run...

8. Operators in Javascript

Updated
•2 min read•View as Markdown

Operators

Operators are the special symbols that is used to perform some operation on oprends in an exporession

  1. Airthmatic operator

    used for basic mathematical operations.

    Examples: + , - , , / , % , **

  2. Assignment operators

    used to assign values to variables.

    Examples: = , += , -= , *= , /=

  3. Comparison operators

    return true or false.

    Examples: == , === , !== , > , < , >= , <=

  4. Logical operators

    Logical operators are used for boolean operations.

    Examples: && , || , !

  5. Unary Operators

    Unary operators operate on a single operand.

    Examples: + , - , ++ , - -

  6. Bitwise operators

    Bitwise operators perform operations on binary numbers.

    Examples: & , | , ^ , ~ , « , »

  7. Conditional (Ternary ) Operator

    Shorthand for if-else.

    condition ? trueValue : falseValue;

  8. Comma operator

    Allows multiple expressions to be evaluated in a single statement.

     let x = (1, 2, 3);
     console.log(x); // 3 (last expression is returned)
    
  9. BigInt Operator

    BigInt (n suffix) is used for large numbers.

     let big = 9007199254740991n + 1n;
     console.log(big); // 9007199254740992n
    
  10. String Operator

    The + operator is used for concatenation.

    let name = "Hello" + " World!";
    console.log(name); // "Hello World!"
    

Interview Questions

  1. What will be the output?

     console.log("5" + 2); // 52
     console.log("5" - 2); //3
    
     console.log(null == undefined);  // true 
     console.log(null === undefined); // false
    
    • "5" + 2 → "52" (concatenation) Implicit(Type Coercion) type conversion in JS

    • "5" - 2 → 3 (numeric conversion)

  2. What is the result of true + true?

    true is converted to 1, so 1 + 1 = 2.

  3. What will be the output ?

      console.log(false ? "Truthy" : "Falsy"); // falsy 
     console.log("" ? "Truthy" : "Falsy"); // falsy 
     console.log(0 ? "Truthy" : "Falsy");// falsy
     console.log(-0 ? "Truthy" : "Falsy");// falsy 
     console.log(null ? "Truthy" : "Falsy");// falsy 
     console.log(NaN ? "Truthy" : "Falsy");// falsy 
     console.log(undefined ? "Truthy" : "Falsy");// falsy 
    
     console.log({} ? "Truthy" : "Falsy"); // Truthy
     console.log([] ? "Truthy" : "Falsy"); // Truthy
     console.log(a ? "Truthy" : "Falsy"); // Truthy
    
  4.   let a = 1;
      let b = (a++, a); // comma operator 
      console.log(b);
    

    Output :

    2

    (a++, a) executes a++, but returns the last expression (a).

  5.   console.log({} == {});  // false
      console.log([] == []);  // false
      console.log([] == ![]); // true
    

    Objects ({} or []) are compared by reference, not value.

Next Topic

Functions in javascript

More from this blog

JAVASCRIPT - Basic to Advance

17 posts

Blog cover all Javascript concepts from beginner to advance level with interview questions