JavaScript Tricky Questions

1. return statement :

function first(){
      return {
       name : "sita"
      };
}
function second(){
      return
      {
         name : "ram"
       };
}

call both the functions

first();
second();

The first() will return the Object =>  { name : "sita"} and the function second() will return undefined both doesn't give the same result because here it adds semicolon for the return statement. It will be inserted immediately at the end of the return statement and the block of the code will never get executed but it doesn't throw any error because the  remainder of the code is valid.

2. Equality :

console.log(0.1 + 0.2 );
console.log(0.1 + 0.2 == 0.3);

Here, our guess goes to true for the second log but it log false. The numbers in JavaScript are treated as floating point. So, unexpectedly the output will be :

0.30000000000000004
false

3. NaN :

NaN means "not a number".
NaN compared to anything or itself returns false.
console.log(NaN == NaN )
output : false
the typeof NaN is number
console.log(typeof NaN == "number")
output :   true

4. If we add strings with numbers : 

console.log(1 +  "2" + "5");
output :
125 // If a string is added after the number then the numbers will be concatenated but not added.

console.log(1 +  +"2" + "5");
output : formula : (  + * + = +)
35 // It string is followed two plus symbols then the first and second numbers are added and the third one is concatenated.

console.log(1 +  -"1" + "5");
output :
35
As formula says ( + * - = -)  here the numbers are subtracted  and the third number is concatenated.

console.log(+"1" +  "5" + "7");
output :
157
Because all are strings.

console.log( "A" - "B" + "5");
output :
NaN5
As, "A"-"B" is not valid so, it returns NaN and the other string 5 is valid it concatenates 5 to NaN.

console.log( "A" - "B" + 5);
output :
NaN
Here, 5 is a number and adding all those is not valid. So, NaN is returned.

5. Boolean false Equality :
console.log(false == '0'); // true
console.log(false === '0'); //false

In JavaScript, we have two equality operators i.e. triple equal and double equal operator. Triple equal will returns true if the two expressions on either side has same type and same value whereas double equal just compares the value.

6.  var y = 10;
var foo =  function() {
console.log(y);
var y = 15;
};
foo();

output :
undefined

The output is not 10 or 15. Then we just get a doubt that if it is not taking a local variable why can't it take global variable the answer is : when the function is getting executed it checks that the local variable y is present but it isn't declared yet so it will not look for the global variable.

7. typeof
 typeof undefined == typeof NULL

output :

true

Because NULL is treated as any other undefined value.

8. console.log(typeof typeof 10);

what would this returns?

output :
string
Because the typeof 10 will result "number" and the typeof "number" will result in string.

9. Delete operator : 
var output = (function(y){
    delete y;
    return y;
  })(0);
 
  console.log(output);

output :
0

Because here delete operator is used delete properties from an object. As y is a local variable, it is not an object. So, no changes will be done.

10. Deleting an array element :
var sample = ["apple","banana","grapes","kiwi","orange"];
delete sample[3];
console.log(sample.length);

output :
5

After deleting the sample[3] it creates an empty space but doesn't get effected to the length of the array. Only the value gets removed and undefined will be placed at that index.

No comments:

Post a Comment

Overview of AngularJS

AngularJS is an open source web application framework. It is now maintained by Google . The most of the code can be eliminated by using th...