tags:

views:

168

answers:

3

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
You forgot Functions.
Peter Bailey
And there are special-purpose objects like Dates.
Nosredna
Good point, Peter Bailey--functions are objects.
Nosredna
Functions are Objects in JavaScript, but yeah.
Magnar
Peter Bailey
Keep in mind that `null` is always reported as `Object` when passed into the `typeof` function.
Dan Herbert
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
You didn't mention arrays.
Nosredna
@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
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
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
Are objects considered primitive types?
CodeToGlory
Would NaN also be considered as a type?
Ben Rowe
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
+1  A: 

There are five primitive data types in JavaScript:

  1. number
  2. string
  3. boolean
  4. undefined
  5. null

Everything that is not a primitive is an object.

CodeToGlory