Test Your JavaScript Skills : ES11 (JavaScript 2020) - 40

Photo by Andrew Neel on Unsplash

Test Your JavaScript Skills : ES11 (JavaScript 2020) - 40

Part 40

Table of contents

Introduction

Namaste, In this blog I will discuss 10 code scenarios on Javascript .

  1. Identify the output of the below program

     let obj = {
         name: {
             firstname : "ram",
             lastname : "kumar"
         }
     }
     console.log(obj.name.middlename.name);
    

    a.undefined

    b.null

    c.crash

    d.kumar

    e.ram

    Answer:c

    Reason:It crashes because middlename.name does not exist on obj.name.

  2. Identify the output of the below program

     let arr =null;
     console.log(arr[1]);
    

    a.null

    b.undefined

    c.crash

    d.1

    e.0

    Answer:c

    Reason:The program crashes because arr is initialized to null.

  3. Identify the output of the below program

     let fx = () => console.log("fx");
     fx = null;
     fx?.();
    

    a.null

    b.fx

    c.undefined

    d.no output

    e.crash

    Answer:d

    Reason: Since fx is initialized to null the function is not called (but does not crash).

  4. Identify the output of the below program

     let num = null
      num = num ?? 5
     console.log(num)
    

    a.null

    b.undefined

    c.error

    d.5

    e.no output

    Answer:d

    Reason:Nullish operator returns 5 as the num is null.

  5. Identify the output of the below program

     const big = 1234124123412412341234123412341234n;
     console.log(typeof big);
    

    a.int

    b.number

    c.float

    d.error

    e.bigint

    Answer:e

    Reason:A integer value suffixed by n is bigint type.

  6. Identify the output of the below program

     let big = BigInt(45252345235252352353523534523453252345235235345);
     console.log(typeof big);
    

    a.number

    b.int

    c.bigint

    d.error

    e.float

    Answer:c

    Reason:A variable assigned by function BigInt is bigint type.

  7. Identify the output of the below program

     let obj = {
         name: {
             firstname : "ram",
             lastname : "kumar"
         }
     }
     console.log(obj.name.middlename?.name);
    

    a.null

    b.ram

    c.crash

    d.undefined

    e.kumar

    Answer:d

    Reason:The optional chaining operator returns undefined for non existent properties.

  8. Identify the output of the below program

     let arr =null;
     console.log(arr?.[1]);
    

    a.null

    b.undefined

    c.1

    d.crash

    e.no output

    Answer:b

    Reason:For non existent index access the optional chaining operator returns undefined.

  9. Identify the output of the below program

    
     let obj = {
         name: {
             firstname : "ram",
             lastname : "kumar"
         }
     }
    
     console.log(obj.name.middlename?.name ?? "Default");
    

    a.undefined

    b.null

    c.error

    d.ram

    e.Default

    Answer:e

    Reason:By computation the value displayed is Default.

  10. Identify the output of the below program

    let obj = {
        name: {
            firstname : "ram",
            lastname : "kumar"
        }
    }
    console.log(obj.name.middlename ?? "default");
    

    a.ram

    b.kumar

    c.undefined

    d.default

    e.error

    Answer:d

    Reason:Since middlename property is non existent default is printed.

Did you find this article valuable?

Support Programming with Mahavir by becoming a sponsor. Any amount is appreciated!