Javascript Interview Questions(Part 3) – User friendly Tech help

Here comes more JavaScript questions with solutions.
n
Learn more share more, do follow us on FB, Twitter , G+ or LinkedIn.
n
n
What does NaN function do?
nThe 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.
n
nThus 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.” 
n
Examples:n


n

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

n

isNaN(true);  // false
isNaN(null); // false
isNaN(37); // false
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

n

 is not NaN
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

n

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

n

n

n

n

n

n

n

n

falsy type

n

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

n

n

n

n

n

n

n

n

true type

n

nWhat will be the output of the following JS codes?
n
n1.
nvar b = 0.6
nvar c= 0.2
n(a-b) == c ??
n
nfalse because float result will always be different
n
n2. ‘ ‘ == 0 ??
n
It will convert string to number and then perform comparison . If we blank string to number, it will return zero.
nSo it becomes 0 == 0 ,which will return true
n
n3. [ ] == 0; ??
n
It will convert []to number and then perform comparison . If we blank [] to number, it will return zero.
nYou can check this by Number([]) will return  0.
nSo it becomes 0 == 0 ,which will return true
n
n4. [ ] == [ ] ??
n
false because both will have different memory pointers.
n
n5. null == null / null=== null??
n
true
n
n6. null == undefined ??
n
ntrue
n
n7. null === undefined??
n
false. Because both have different types.
ntypeof(null) > object (It is a bug and cannot be fixed because it will break
ntypeof(undefined) > undefined
n
n8. null instanceOf object ?? 
nit will throw an error
n
n9. Is ‘false’  return false?
n
No ,Because it is a string with length greater than 0.Empty string return false
n
10. Is ‘ ‘ return false?
n
No ,Because it is a string with an empty space.
n
n11. What is the output of void(2+3)?
nIt will throw an error.

n

12. [0] == true

n

False

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

n

13. “ufthelp” == true

n

False

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

n

Difference between null and  undefined? 
nnull 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.
nEx:
nvar test_var = null;
nalert(test_var); //show null
nalert(type of test_var); //shows object
nNote: 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”.

n

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

n


n

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.
n
What are the scenarios to get undefind?

n

    n

  • A declared variable without assigning any value to it.Ex: var a;
  • n

  • Implicit returns of functions due to missing return statements
  • n

  • Return statements that explicitly do not return anything
  • n

  • Function parameters that are not passed 
  • n

  • Lookups for non-existent properties that do not exist
  • n

  • Anything that has been set to undefined
  • n

  • Any expression in the form of void
  • n

n

  Deep dive into null

Was this article helpful?
YesNo

Similar Posts