[Javascript] Javascript Quiz 1 !!!


Try to answer the following questions!!


1.  True or False?
var a = true;
    if(a == 1){
        console.log("True");
    }else{
        console.log("False");
    }


2. True or False?
var a = true;
    if(a === 1){
        console.log("True");
    }else{
        console.log("False");
    }


3.  What will be the output of the following program?
function myFunction() {
    return
    {
        a : 0
    }
}
console.log(myFunction());


4. What will be the output of the following program?
var a = 110;
var b = 012;

console.log(a+b);


5.What will be the output of the following program?
var myVariable = 10;

function myFunction() {
    myVariable = 25;
    var myVariable;
}
myFunction();

console.log(myVariable);


For the solution, Please check the comment section!! If you need clarification for any of the above problem. Please let me know in the comment section.


Comments

  1. 1. True
    2. False
    3. Undefined
    4. 120
    5. 10

    Try it in your javascript editor and share your comments.!!

    ReplyDelete
  2. Care to explain number 5? Does calling var myVariable; inside the function redirect the reference to the original reference?

    ReplyDelete
    Replies
    1. Actually, Javascript first creates variable and set the undefined value to it. Values will be set to the variable during the time of execution.

      Global scope:

      var myVariable = undefined;

      function scope:

      var myVariable = undefined;

      Then while running the code, inside the function javascript complier checks for the variable myVariable and already it is present with undefined value so myVariable in the outer scope is not affected.

      Delete
    2. Of course! Hoisting and compiling. Thanks :)

      Delete

Post a Comment