javaScript Classroom image

sumit  kandwal / Professional / Web Technology
 
To post your Question Join Classroom
 
Question No  (1 of 4)
 1 I 2 I 3 I 4 Last
 
Question:-
What will the code below output to the console and why? (function(){ var a = b = 3; })(); console.log("a defined? " + (typeof a !== 'undefined')); console.log("b defined? " + (typeof b !== 'undefined'));
 
Answer:-

typeof bar === "object" is the way of checking if bar is an object, but in JavaScript that null is also considered an object.

Therefore, the following code will surprise most of developers, log true (not false) to the console:

var bar = null;
console.log(typeof bar === "object");  // logs true!
As long as one is aware of this, the problem can easily be avoided by also checking if bar is null:

console.log((bar !== null) && (typeof bar === "object"));  // logs false

 there are two other things :

1) the above solution will return false if bar is a function. In most cases, this is the desired behavior, but in situations where you want to also return true for functions code will be:

console.log((bar !== null) && ((typeof bar === "object") || (typeof bar === "function")));

2) the above solution will return true if bar is an array (e.g., if var bar = [];). In most cases, this is the desired behavior, since arrays are indeed objects, but in situations where you want to also false for arrays, code will be:

console.log((bar !== null) && (typeof bar === "object") && (toString.call(bar) !== "[object Array]"));

typeof bar === "object" is the way of checking if bar is an object, but in JavaScript that null is also considered an object.

Therefore, the following code will surprise most of developers, log true (not false) to the console:

var bar = null;
console.log(typeof bar === "object");  // logs true!
As long as one is aware of this, the problem can easily be avoided by also checking if bar is null:

console.log((bar !== null) && (typeof bar === "object"));  // logs false

 there are two other things :

1) the above solution will return false if bar is a function. In most cases, this is the desired behavior, but in situations where you want to also return true for functions code will be:

console.log((bar !== null) && ((typeof bar === "object") || (typeof bar === "function")));

2) the above solution will return true if bar is an array (e.g., if var bar = [];). In most cases, this is the desired behavior, since arrays are indeed objects, but in situations where you want to also false for arrays, code will be:

console.log((bar !== null) && (typeof bar === "object") && (toString.call(bar) !== "[object Array]"));

typeof bar === "object" is the way of checking if bar is an object, but in JavaScript that null is also considered an object.

Therefore, the following code will surprise most of developers, log true (not false) to the console:

var bar = null;
console.log(typeof bar === "object");  // logs true!
As long as one is aware of this, the problem can easily be avoided by also checking if bar is null:

console.log((bar !== null) && (typeof bar === "object"));  // logs false

 there are two other things :

1) the above solution will return false if bar is a function. In most cases, this is the desired behavior, but in situations where you want to also return true for functions code will be:

console.log((bar !== null) && ((typeof bar === "object") || (typeof bar === "function")));

2) the above solution will return true if bar is an array (e.g., if var bar = [];). In most cases, this is the desired behavior, since arrays are indeed objects, but in situations where you want to also false for arrays, code will be:

console.log((bar !== null) && (typeof bar === "object") && (toString.call(bar) !== "[object Array]"));

Prev   
 
 
 
image
sumit  kandwal

Skills    javaScript

Qualifications :- High School - GIC, College/University - Graphic Era University,
Location :-Dehradun,Dehradun,UTTRAKHAND,
Description:- Having 10+ years of experience in Software development.
Explore
 

  Students (9)

Ask 
image
Pooja
Ask 
Neha
Ask 
image
Amit
Ask 
Pankaj
Ask 
Pankaj
Ask 
image
Ajay
Ask 
image
Neeraj
Ask 
image
Anil
Ask 
image
sumit
 
 

Recommended Classes

Rajendra Kumar
I have more then 5 year of Mobile App Development Experience and have worked in Android Native, I...
Professional
image Anil Bist
in 2006, i Join a web development company in Bangalore and start work on "Ruby on Rail"...
Professional
image Sandeep Srivastava
I am working as software develop from last 9 year.worked in Java, Struts, SpringMVC, Hibernate.
Professional