Wednesday, February 28, 2018

Java script tricky questions and answers part II

1. What is the value of  "10"+3+4+3

Answer : "10343"

Explanation:
                  Because of first one "10" identified as string. So remaining also could be identified as string only.

Things To Remember:
                  First character is the matter here. If first one is string remaining will consider as string only.




2. Output of the below and explain why ?


var output = (function(x){
    delete x;
    return x;
  })(0);
  
  console.log(output);

Answer : 0.

Explanation: 
                 Delete operator will delete properties from an object. Here x mentioned as local variable.

Things To Remember:
                 Delete operator will not affect local variable.




3. Output of the below and explain why ?


var letters = ["A","B","C","D","E"];
delete letters[3];
  
  console.log(letters.length);

Answer: 5

Explanation:

                  When you delete an elements from an array by using delete operator, the length will not get affect.

Things To Remember:

                 The deleted value will be replaced by undefined and length will not get change.





4. Check below code and tell what is the output ?


console.log(false == '0')
console.log(false === '0')

Answer: true
               false

Explanation:
         
                 Here == and === doing the magic here. This is what, why we need to use always === if you want check equal value please do check its type also.

Things To Remember:

                 Always use === to check value and its type.




5. Explain below code output and why ?

(function(x) {
    return (function(y) {
        console.log(x);
    })(2)
})(1);

Answer: 1

Explanation :

                 Closure is the reason here, why its printing number 1 as output. Here x is defined outside of the inner function and closure have capability to get those value. So printing no 1 as output.

Things To Remember:

                  Closure function.




6. Check below code snippet and explain why ?

console.log(1 < 2 < 3);
console.log(3 > 2 > 1);

Answer: true
               false

Explanation:

                We can analyse the second log message why its return false. Then you can go ahead to compare first one why its returning true.

3>2 will return true. After that, we know true can consider as 1. So now 1>1 which could return false.

Things To Remember:

True can consider as 1 too.




7. Below code is consider as number or string or any other type ?

console.log(+'dude');

Answer: NaN

Explanation:

                Because of + symbol compiler will consider as adding operation (unary operator) so it will take dude as a number and trying to add. But It can not. So telling NaN.

Things To Remember:

                 First character is the matter here.




8. Explain following code result.

var test = "testingHoist";

 (function () {
     console.log("The value is " + test);

     var test = "testingHoistXXXXXXX";

     console.log("Latest value is " + test);
})();

Answer: The value is undefined
               Latest value is testingHoistXXXXXXX

Explanation:
       
               Because of hoist these results we got undefined.

Things To Remember:
   
                Functions and variables are hoisted in java script.




9. Check the below code and explain why?
               

var testObject = {
    foo: "bar",
    func: function() {
        var self = this;
        console.log("outer func first:  this.foo = " + this.foo);
        console.log("outer func second:  self.foo = " + self.foo);
        (function() {
            console.log("inner func first:  this.foo = " + this.foo);
            console.log("inner func second:  self.foo = " + self.foo);
        }());
    }
};
testObject.func();

Answer: outer func first:  this.foo = bar
               outer func second:  self.foo = bar
               inner func first:  this.foo = undefined
               inner func second:  self.foo = bar

Explanation :

               Because of "this" keyword we are getting this output. You should always assign this to self like above. So that you can access outer variables. But if you know ES6 arrow function you no need to worry about this.

Things To Remember:

               You should always assign to self (this=self) in ES5, so that you can access varibles. But ES6 solved this problem.





10. Explain below code

console.log(0.5 + 0.3);
console.log(0.4 + 0.2 == 0.6);

Answer: 0.8
               false

Explanation :

               Dont expect always it return the correct sum value and true. Java script would consider this as floating point precision , so it may tend to provide any wrong results.

Things To Remember:

               For this type of calculation work, please go with Math.So that it will solve your problem.






1 comment:

  1. I like your post. It is good to see you verbalize from the heart and clarity on this important subject can be easily observed... 经济代写

    ReplyDelete