0% found this document useful (0 votes)
176 views

Better Way To Get Type of A Javascript Variable - Stack Overflow

The document discusses different ways to determine the type of a JavaScript variable. It asks if there is a better way than typeof, and several responses are provided offering alternative approaches like using Object.prototype.toString or constructor.name. The discussion also notes limitations of certain methods in older browsers.

Uploaded by

madscijr
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
176 views

Better Way To Get Type of A Javascript Variable - Stack Overflow

The document discusses different ways to determine the type of a JavaScript variable. It asks if there is a better way than typeof, and several responses are provided offering alternative approaches like using Object.prototype.toString or constructor.name. The discussion also notes limitations of certain methods in older browsers.

Uploaded by

madscijr
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

12/12/13

Better way to get type of a Javascript variable? - Stack Overflow


sign up log in tour help careers 2.0

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Tell me more

Better way to get type of a Javascript variable?

Is there a better way to get the type of a variable in JS than t y p e o f? It works fine when you do: >t y p e o f1 " n u m b e r " >t y p e o f" h e l l o " " s t r i n g " But it's useless when you try: >t y p e o f[ 1 , 2 ] " o b j e c t " > r=n e wR e g E x p ( / . / ) / . / >t y p e o fr " f u n c t i o n " I know of i n s t a n c e o f, but this requires you to know the type beforehand. >[ 1 , 2 ]i n s t a n c e o fA r r a y t r u e >ri n s t a n c e o fR e g E x p t r u e Is there a better way?
javascript types typeof

asked Sep 12 '11 at 15:39 Aillyn 4,939 2 24 56 FYI, the t y p e o fn e wR e g E x p ( / . / ) ;/ /" f u n c t i o n " issue in Chrome appears to be fixed in Chrome 14. user113716 Sep 12 '11 at 18:36 possible duplicate of How do I get the name of an object's type in JavaScript? Aillyn Sep 12 '11 at 18:45 add comment

6 Answers
Angus Croll recently wrote an interesting blog post about this http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/ He goes through the pros and cons of the various methods then defines a new method 'toType' v a rt o T y p e=f u n c t i o n ( o b j ){ r e t u r n( { } ) . t o S t r i n g . c a l l ( o b j ) . m a t c h ( / \ s ( [ a z A Z ] + ) / ) [ 1 ] . t o L o w e r C a s e ( ) }
answered Sep 12 '11 at 15:53 ipr101

stackoverflow.com/questions/7390426/better-way-to-get-type-of-a-javascript-variable

1/4

12/12/13
ipr101 14.1k 2 21 39

Better way to get type of a Javascript variable? - Stack Overflow

2 1

Interesting side note here - this could potentially break where a "host" object is passed to the function. Internet Explorer A c t i v e X O b j e c t instances, for example. Andy E Sep 12 '11 at 16:04 Really tricky... CDT Apr 29 at 8:39 If you created your own object type (prototypal inheritance), how could you get it to return a more specific value than "object"? This was also raised as an issue here, but was never addressed. EleventyOne Jul 27 at 19:30

add comment

A reasonably good type capture function is the one used by YUI3:

v a rT Y P E S={ ' u n d e f i n e d ' :' u n d e f i n e d ' , ' n u m b e r ' :' n u m b e r ' , ' b o o l e a n ' :' b o o l e a n ' , ' s t r i n g ' :' s t r i n g ' , ' [ o b j e c tF u n c t i o n ] ' :' f u n c t i o n ' , ' [ o b j e c tR e g E x p ] ' :' r e g e x p ' , ' [ o b j e c tA r r a y ] ' :' a r r a y ' , ' [ o b j e c tD a t e ] ' :' d a t e ' , ' [ o b j e c tE r r o r ] ' :' e r r o r ' } , T O S T R I N G=O b j e c t . p r o t o t y p e . t o S t r i n g ; f u n c t i o nt y p e ( o ){ r e t u r nT Y P E S [ t y p e o fo ]| |T Y P E S [ T O S T R I N G . c a l l ( o ) ]| |( o?' o b j e c t ':' n u l l ' ) ; } ; This captures many of the primitives provided by javascript, but you can always add more by modifying the T Y P E S object. Note that t y p e o fH T M L E l e m e n t C o l l e c t i o n in Safari will report f u n c t i o n, but type(HTMLElementCollection) will return o b j e c t
answered Sep 12 '11 at 15:49 Nick Husher 1,118 2 10 add comment

You can try using c o n s t r u c t o r . n a m e. [ ] . c o n s t r u c t o r . n a m e n e wR e g E x p ( ) . c o n s t r u c t o r . n a m e As with everything JavaScript, someone will eventually invariably point that this is somehow evil, so here is a link to an answer that covers this pretty well. An alternative is to use O b j e c t . p r o t o t y p e . t o S t r i n g . c a l l O b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( [ ] ) O b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( / . / )
edited Sep 12 '11 at 15:46 answered Sep 12 '11 at 15:41 Xeon06 14.7k 1 30 76

stackoverflow.com/questions/7390426/better-way-to-get-type-of-a-javascript-variable

2/4

12/12/13

Better way to get type of a Javascript variable? - Stack Overflow


14.7k 1 30 76

n a m e is an extension to ECMAScript and will not work in all browsers. Andy E Sep 12 '11 at 15:43

add comment

Also we can change a little example from ipr101 O b j e c t . p r o t o t y p e . t o T y p e=f u n c t i o n ( ){ r e t u r n( { } ) . t o S t r i n g . c a l l ( t h i s ) . m a t c h ( / \ s ( [ a z A Z ] + ) / ) [ 1 ] . t o L o w e r C a s e ( ) } and call as " a a a " . t o T y p e ( ) ;/ /' s t r i n g '
answered Nov 8 '12 at 13:52 greymaster 41 1 add comment

You can apply O b j e c t . p r o t o t y p e . t o S t r i n g to any object: v a rt o S t r i n g=O b j e c t . p r o t o t y p e . t o S t r i n g ; c o n s o l e . l o g ( t o S t r i n g . c a l l ( [ ] ) ) ; / / >[ o b j e c tA r r a y ] c o n s o l e . l o g ( t o S t r i n g . c a l l ( / r e g / g ) ) ; / / >[ o b j e c tR e g E x p ] c o n s o l e . l o g ( t o S t r i n g . c a l l ( { } ) ) ; / / >[ o b j e c tO b j e c t ] This works well in all browsers, with the exception of IE - when calling this on a variable obtained from another window it will just spit out [ o b j e c tO b j e c t ].
answered Sep 12 '11 at 15:46 Andy E 112k 19 212 266

11 I wish IE would just die. Xeon06 Sep 12 '11 at 15:50 1 2 1


@Xeon I think the hatred against IE is a bit too much. IE9 is a much better browser than its predecessors. Aillyn Sep 12 '11 at 15:58 @pessimopoppotamus but nobody who would update their browser to IE9 uses IE. Xeon06 Sep 12 '11 at 16:01 IE 9 is a step in the right direction, IE 10 is looking pretty good. Incidentally, the bug I mentioned was a regression in IE 9 - they fixed it in IE 8. Andy E Sep 12 '11 at 16:05

add comment

one line function :

f u n c t i o nt y p e ( v a l ) { O b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( v a l ) . r e p l a c e ( / ^ \ [ o b j e c t( . + ) \ ] $ / , " $ 1 " ) . t o L o w e r C a s e ( ) ; } this give the same result as j Q u e r y . t y p e ( )


edited Jan 2 at 14:30 answered Oct 31 '12 at 16:57 Yukull 611 2 10

add comment

stackoverflow.com/questions/7390426/better-way-to-get-type-of-a-javascript-variable

3/4

12/12/13

Better way to get type of a Javascript variable? - Stack Overflow

Not the answer you're looking for? Browse other questions tagged javascript types
typeof or ask your own question.

stackoverflow.com/questions/7390426/better-way-to-get-type-of-a-javascript-variable

4/4

You might also like