Your SlideShare is downloading. ×
JavaScript typeof and instanceof – the concept and differences
JavaScript typeof and instanceof – the concept and differences
JavaScript typeof and instanceof – the concept and differences
JavaScript typeof and instanceof – the concept and differences
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

JavaScript typeof and instanceof – the concept and differences

236

Published on

As a JavaScript developer you must have used the typeof and the instanceof operator sometime in your code to detect the the type of a variable in your program logic. And most often or not you might …

As a JavaScript developer you must have used the typeof and the instanceof operator sometime in your code to detect the the type of a variable in your program logic. And most often or not you might have been confused with the different results that show up while expecting a desired result. In this post I am gonna show you an easy technique to determine the type and instance of a variable. Let’s see some examples first.
For the full post download this file or visit the actual blog post link: http://jbkflex.wordpress.com/2013/05/09/javascript-typeof-and-instanceof-the-concept-and-differences/

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
236
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. JavaScript typeof and instanceof –the concept and differencesFor the actual blog post link visit : http://jbkflex.wordpress.com/2013/05/09/javascript-typeof-and-instanceof-the-concept-and-differences/As a JavaScript developer you must have used the typeof and the instanceof operator sometime in your code todetect the the type of a variable in your program logic. And most often or not you might have been confused withthe different results that show up while expecting a desired result. In this post I am gonna show you an easytechnique to determine the type and instance of a variable. Let’s see some examples first.The typeof operatorvar str1 = "Hello, how are you?";typeof str1;Output: "string"So, we have a string literal str1, and when we find the typeof str1, it gives “string”. Remember string is a primitivedata type in JavaScript. Let’s see another example,var str2 = new String("I am doing good. Thank you!");typeof str2;Output: "object"Now, we have a String Object and we have created an instance of that Object using the new operator, which is str2.Now if we do a typeof, it gives object. Remember that in JavaScript, other than the primitive data types (and null,undefined, function), everything else is an Object. So as expected you get the type as object.Now, let’s work with Array’s and try the same thing,var arr1 = [2,3,4]; //defining an array literaltypeof arr1;Output: "object"
  • 2. var arr2 = new Array(4,5,6); //creating an Array Object instancetypeof arr2;Output: "object"Firstly, we have an Array literal – arr1 and if we do a typeof it gives “object”. Why? Since Array’s are Objects andthey are not part of the primitive data set.The next example is more obvious, we create an Array Object instance – arr2, and then do a typeof. As expected weget a “object”. So we have seen, if the typeof does not return a primitive data type (which are number, boolean,string), it will return an object. There are some special cases though, look at the bottom of this page to find out.The instanceof operatorNow let’s take up some examples of the instanceof operator.var str1 = "Hello, how are you?"; //defining a string literal, same as abovestr1 instanceof String;Output: falseIf you see above, we have a string literal and when we do a instanceof to check if the variable is an instance of theString Object, it returns false. Since a string literal belongs to the string primitive data type. Now, consider theexample below,var str2 = new String("I am doing good. Thank you!"); //defining a Stringobject instancestr2 instanceof String;Output: trueHere, str2 is actually an instance, so the test returns true.But What’s up with the Array’s though? Let’s see,var arr1 = [2,3,4]; //defining an array literalarr1 instanceof Array;Output: true
  • 3. var arr2 = new Array(4,5,6); //creating an Array Object instancearr2 instanceof Array;Output: trueBoth returns true as both arr1 and arr2 are Array objects and they are not part of the primitve data set.In fact the following test also returns truearr1 instanceof Object;Output: truearr2 instanceof Object;Output: trueThis is because the Array object inherits from the more generic Object object. (Yes, you heard it right Objectobject!).Still confused about finding types and instances? Here is the rule to do it easily.Rule for the testSo, how do you guess if a variable is an instance of its Object or Type. Here is the rule,1) First check the type of the variable using typeof.2) Now do the instanceof test.3) a) If the typeof test returns a primitive type – number, boolean, string. Then instanceof test will return false.Because these are just primitive type data. They are not instances of any Object.b) But if typeof returns an object, then instanceof test will return true. So you know that you are dealing withobjects and instances.For eg.var regexp = /d{3}/; //define a regular expression objecttypeof regexp; -> "object", not a primitive type
  • 4. regexp instanceof RegExp; -> trueregexp instanceof Object; -> trueTake another,var str = "Hello, me again!";typeof str; -> "string" , a primitive typestr instanceof String; -> falseNow, you can think of some examples and put the rule to a test.Special cases1) typeof null = “object” , since null is a special value in JavaScript which represents emptyness and it does nothave any type.2) typeof undefined = “undefined”, as expected3) typeof NaN = number, since NaN is actually a special number, although it says Not a Number. Confusing? Justkeep in mind.4) typeof Function = “function”5) typeof xmlObj = “xml”References1) typeof operator : https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/typeof2) instanceof operator: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/instanceof

×