Test your JavaScript Skills:Fundamentals-6

Part 6

Introduction

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

Video Tutorial

  1. Identify the output of the below program

     var i=100;
     var i=20;
     console.log("value", i);
    

    a.20

    b.100

    c.error

    d. 120

    e.undefined

    Answer: a

    Reason: The 'var' allows the redeclaration of a variable.

  2. Identify the output of the below program

     console.log("value", myval);
     var myval=20;
    

    a.20

    b.error

    c.undefined

    d. null

    e.0

    Answer: c

    Reason: Since 'var' performs hoisting the value is undefined.

  3. Identify the output of the below program

     function fx() {
      console.log("value", myval);
     }
     fx();
     var myval=10;
    

    a.undefined

    b.20

    c.error

    d. null

    e.0

    Answer: a

    Reason: Since 'var' performs hoisting the value is undefined.

  4. Identify the output of the below program

     function fx() {
         console.log("value", myval+myval2);
        }
        fx();
        var myval=10;
        var myval2=20;
    

    a.error

    b.30

    c.undefined

    d. NaN

    e.null

    Answer: d

    Reason: After hoisting the value undefined is coerced to a number which results in NaN hence NaN+NaN is NaN.

  5. Identify the output of the below program

     var j=20;
     if (j > 3) {
      var i=3;
     }
     console.log("value", i);
    

    a.20

    b.3

    c.undefined

    d. error

    e.NaN

    Answer: b

    Reason: Since the 'var' declaration does not have a block scope variable can be accessed outside the block.

  6. Identify the output of the below program

     var j=20;
     if (j > 3) {
      var i=3;
     }
    
     function fx() {
         console.log("value", i);
     }
        fx();
    

    a.20

    b.undefined

    c.3

    d. error

    e.NaN

    Answer: c

    Reason: Since the 'var' declaration does not have a block scope variable can be accessed outside the block.

  7. Identify the output of the below program

     for (var i=0; i< 4;) {
         i++;
     }
    
     console.log("value", i);
    

    a.null

    b.undefined

    c.NaN

    d. 3

    e.4

    Answer: e

    Reason: Since the 'var' declaration does not have a block scope variable can be accessed outside the block.

Video Tutorial

Did you find this article valuable?

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