30

What are JavaScript Data Types?

Yves
  • 12,059
  • 15
  • 53
  • 57

3 Answers3

25

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
  • 28,550
  • 8
  • 60
  • 65
  • And there are special-purpose objects like Dates. – Nosredna Aug 17 '09 at 15:33
  • Good point, Peter Bailey--functions are objects. – Nosredna Aug 17 '09 at 15:34
  • Functions are Objects in JavaScript, but yeah. – Magnar Aug 17 '09 at 15:34
  • 2
    @Nosredna & @Magnar - Unless I'm sorely mistaken, no they aren't: `alert( typeof function(){} )` vs `alert( typeof {} )` – Peter Bailey Aug 17 '09 at 15:42
  • Keep in mind that `null` is always reported as `Object` when passed into the `typeof` function. – Dan Herbert Aug 17 '09 at 15:50
  • 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 Aug 17 '09 at 15:53
  • @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 Aug 17 '09 at 15:59
  • 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 Aug 17 '09 at 20:50
  • 4
    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 Aug 17 '09 at 22:18
  • Are objects considered primitive types? – Srikar Doddi Aug 24 '10 at 02:22
  • Would NaN also be considered as a type? – Ben Rowe Aug 24 '10 at 02:56
17

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.

Srikar Doddi
  • 15,499
  • 15
  • 65
  • 106
  • Isn't null an empty object used to initialize object that don't have values? Why is it still considered a primitive type? -- sorry noob here – anpatel Mar 12 '15 at 15:38
2

Javascript has a typeof operator that returns a string that names the datatype of its argument. So, you could say that the datatypes in Javascript are all the possible return values of this operator. Look here:

https://developer.mozilla.org/en/JavaScript/Reference/Operators/typeof

Anshul
  • 9,312
  • 11
  • 57
  • 74
csusbdt
  • 96
  • 7