be careful about null
in javascript, defined those 5 types as primitive.
undefined
null
-
boolean
:true
/false
-
number
:NaN
/-Infinity
/Infinity
/0
,1
,2
... /1.0e+3
/ etc ... -
string
:''
/""
/'hoge'
/"huga"
/ etc ...
however, be careful about null
value.
typeof null // "object" (!!)
null instanceof Object // false (!!)
typeof {} // "object"
{} instanceof Object // true
ES.next fixes this oddly specification. ECMAScript 6 is available this syntax: typeof null === 'null'
.
But today, typeof null
return 'object'.
var isPrimitive = function(any) {
switch (typeof any) {
case 'undefined':
case 'boolean':
case 'string':
case 'number':
return true;
default: // typeof any #=> 'object'
return any === null;
}
Written by VoQn
Related protips
1 Response
Test Markdown syntax comment
isPrimitive(true); // true
isPrimitive(0); // true
isPrimitive(null); // true
isPrimitive([]); // false
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Javascript
Authors
Related Tags
#javascript
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#