Categories
Uncategorized

Javascript Interview Questions(Part 3)

Here comes more JavaScript questions with solutions.

Learn more share more, do follow us on FB, Twitter , G+ or LinkedIn.

Javascript interview Questions
What does NaN function do?
The isNaN() function determines whether a value is NaN or not. Coercion inside the isNaN function has interesting rules.When the argument to the function is not isNaN() of type Number.The value is first coerced to a Number. The resulting value is then tested to determine whether it is NaN.

Thus for non-numbers that when coerced to numeric type result in a valid non-NaN numeric value (notably the empty string and Boolean primitives, which when coerced give numeric values zero or one), the “false” returned value may be unexpected; the empty string, for example, is surely “not a number.” 

Examples:


isNaN(NaN);       // true
isNaN(undefined); // true
isNaN({}); // true

isNaN(true);      // false
isNaN(null); // false
isNaN(37); // false

// strings
isNaN("37"); // false: "37" is converted to the number 37 which is not NaN
isNaN("37.37"); // false: "37.37" is converted to the number 37.37 which is not NaN
isNaN(""); // false: the empty string is converted to 0 which is not NaN
isNaN(" "); // false: a string with spaces is converted to 0 which 
                         is not NaN

// dates
isNaN(new Date()); // false
isNaN(new Date().toString()); // true

isNaN("blabla") // true: "blabla" is converted to a number.
// Parsing this as a number fails and returns NaN



falsy: In javascript 6 things are falsy and they are- false, null, undefined, ”, 0, NaN

NaN in JavaScript
falsy type


truthy: There are only two truthy things- true and everything that is not false

JavaScript true type in NaN Function
true type


What will be the output of the following JS codes?

1.
var b = 0.6
var c= 0.2
(a-b) == c ??

false because float result will always be different

2. ‘ ‘ == 0 ??

It will convert string to number and then perform comparison . If we blank string to number, it will return zero.
So it becomes 0 == 0 ,which will return true

3. [ ] == 0; ??

It will convert []to number and then perform comparison . If we blank [] to number, it will return zero.
You can check this by Number([]) will return  0.
So it becomes 0 == 0 ,which will return true

4. [ ] == [ ] ??

false because both will have different memory pointers.

5. null == null / null=== null??

true

6. null == undefined ??

true

7. null === undefined??

false. Because both have different types.
typeof(null) > object (It is a bug and cannot be fixed because it will break
typeof(undefined) > undefined

8. null instanceOf object ?? 
it will throw an error

9. Is ‘false’  return false?

No ,Because it is a string with length greater than 0.Empty string return false

10. 
Is ‘ ‘ return false?

No ,Because it is a string with an empty space.

11. What is the output of void(2+3)?
It will throw an error.

12. [0] == true

False

//How it works
//convert boolean using toNumber
[0] == 1
//convert object using toPrimitive
//[0].valueOf() is not a primitive so use
//[0].toString() –> “0”
“0” == 1
//convert string using toNumber
0 ==1 –> False


13. “ufthelp” == true

False

//How it works
//convert boolean using toNumber
“ufthelp” == 1
//convert string using toNUmber
NaN == 1 –> False


 

Difference between null and  undefined? 
null means empty or non-existent assignment value which is used by programmers to indicate “no value”. null is a primitive value and you can assign null to any variable.
Ex:
var test_var = null;
alert(test_var); //show null
alert(type of test_var); //shows object
Note: null is not an object,it is a primitive value.Sometimes people assume it wrongly that you can add properties to it because it typeof(null) returns “object”.

undefined means ,undefined means a variable has been declared but has not yet been assigned a value.
Ex:
var test_var;
alert(test_var); //show undefined
alert(type of test_var); //shows undefined


Remember,undefined is a type with exactly one value and that is undefined.You cannot change the value of it by assigning a new value.

What are the scenarios to get undefind?

  • A declared variable without assigning any value to it.Ex: var a;
  • Implicit returns of functions due to missing return statements
  • Return statements that explicitly do not return anything
  • Function parameters that are not passed 
  • Lookups for non-existent properties that do not exist
  • Anything that has been set to undefined
  • Any expression in the form of void

  Deep dive into null

Leave a Reply

Your email address will not be published. Required fields are marked *