What are JavaScript Data Types?
+6
A:
There's
- Numbers
- Strings
- Booleans
- Objects
- null
- undefined
Note that there isn't a separate Integer, just number - which is represented as a double precision floating point number.
There's also
- Functions
- Arrays
- RegExps
all of which are Objects deep down, but with enough special wiring to be mentioned on their own.
Magnar
2009-08-17 15:32:07
You forgot Functions.
Peter Bailey
2009-08-17 15:33:12
And there are special-purpose objects like Dates.
Nosredna
2009-08-17 15:33:29
Good point, Peter Bailey--functions are objects.
Nosredna
2009-08-17 15:34:01
Functions are Objects in JavaScript, but yeah.
Magnar
2009-08-17 15:34:23
Peter Bailey
2009-08-17 15:42:32
Keep in mind that `null` is always reported as `Object` when passed into the `typeof` function.
Dan Herbert
2009-08-17 15:50:27
According to http://www.crockford.com/javascript/survey.html everything apart from numbers, strings and booleans are objects. They do behave differently enough that they might aswell be listed as a separate datatype. Fixing.
Magnar
2009-08-17 15:53:33
You didn't mention arrays.
Nosredna
2009-08-17 15:57:54
@Peter Bailey. It depends. JavaScript is notorious poor at reflection of its types. I wouldn't go by what it _says_ its types are.
Nosredna
2009-08-17 15:59:24
Arrays are Objects too.. I feel this slope is getting slippery.. should we add Dates and RegExp too now? No, they're objects. I'm about to change my mind on functions again. ;-)
Magnar
2009-08-17 20:50:47
Arrays definitely have their own place in the language. They are objects, but there's a lot of extra rigging built around them. The real answer is that with JavaScript, answers are almost always mushy. Good answers are always followed up with "yes, but" clauses. It's a very flexible language. A shapeshifter, ready to throw on a blonde wig and party dress any night of the week.
Nosredna
2009-08-17 22:18:41
Are objects considered primitive types?
CodeToGlory
2010-08-24 02:22:27
Would NaN also be considered as a type?
Ben Rowe
2010-08-24 02:56:42
A:
- Variant.
That's it. Every variable can contain any kind of data.
For the actual values stored in the varibles, they can be basic objects like Number and String, or other kinds of objects like Regexp or XmlHttpRequest.
Guffa
2009-08-17 15:35:04
+1
A:
There are five primitive data types in JavaScript:
- number
- string
- boolean
- undefined
- null
Everything that is not a primitive is an object.
CodeToGlory
2010-08-24 02:24:21