9

I started reading a book, Javascript for Kids. In it the author states that there are three data types:

  • numbers
  • strings
  • booleans

However, according to W3Schools, there are four:

  • numbers
  • strings
  • arrays
  • objects

I wanted to know which one is correct.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
eagercoder
  • 526
  • 1
  • 10
  • 23

5 Answers5

9

You can test it using typeof operator:

The typeof operator gives you the names of data types when placed before any single operand.

Hence, try using typeof with any operand variable: it will give one of the following datatype names:

  1. String
  2. Number
  3. Boolean
  4. Object
  5. Undefined

Hence, these are the five data Types in Javascript.

var val1 = "New World";   //returns String
var val2 = 5;             //returns Number
var val3 = true;          //returns Boolean
var val4 = [1,2,3];       //returns Object
var val5 = null;          //returns Object (Value is null, but type is still an object)
var val6;                 //returns Undefined
dotancohen
  • 30,064
  • 36
  • 138
  • 197
svik
  • 212
  • 2
  • 12
  • Wouldn't `val4` be considered an array? – eagercoder Jun 25 '15 at 00:07
  • Its value type is definitely an array, but since Javascript can only categorise it as one of the aforementioned datatypes, it places Array into object category. It treats it as a special object though, but definitely under Object. – svik Jun 25 '15 at 00:18
  • @eagercoder No, the _type_ is an object, the _instance_ is of an `Array`. You’ll learn about prototypes, eventually: arrays aren’t really treated in a special way. They are just object that have the `Array` interface and thus supports method like `sort`, `map`, `filter`, etc. Every object is an instance of some function (in this case `function Array()`) and thus implements it as a prototype. – Sebastian Simon Jun 25 '15 at 00:23
  • If you check out http://www.w3schools.com/js/js_datatypes.asp , you will find W3Schools mentioning following line: "In JavaScript, an array is a special type of object. Therefore typeof [1,2,3,4] returns object" @Xufox :you just said what I said, that its type is Object. Although, its not clear from your comment, WHICH type are you talking about. DataType or ValueType? which I just clearly mentioned already to eagercoder that, its Datatype is OBJECT while its Value Type is Array. Simple as that. – svik Jun 25 '15 at 00:28
  • @svik [I wouldn’t listen to W3Schools](http://www.w3fools.com/). There’s absolutely nothing special about an array. The only special thing is one way to write an array literal: `[2,4,6]`. But that has nothing to do with data types, only with syntax. – Sebastian Simon Jun 25 '15 at 00:32
4

Things aren't actually as straightforward as they described in answers above... they usually aren't in javascriptland ;)

typeof is the 'official' function that one uses to get the type in javascript, however in certain cases it might yield some unexpected results ...

1. Strings

typeof "String" or
typeof Date(2011,01,01)

"string"

2. Numbers

typeof 42 or
typeof NaN, lol

"number"

3. Bool

typeof true (valid values true and false)

"boolean"

4. Object

typeof {} or
typeof [] or
typeof null or
typeof /aaa/ or
typeof Error()

"object"

5. Function

typeof function(){}

"function"

6. Undefined

var var1; typeof var1

"undefined"

Alternative is to use ({}).toString() which will get you somewhat more accurate answer most of the time...

Community
  • 1
  • 1
Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265
3

Check the following link

  • Six data types that are primitives:

    1.Boolean

    2.Null

    3.Undefined

    4.Number

    5.String

    6.Symbol (new in ECMAScript 6)

  • and Object

Fawzan
  • 4,738
  • 8
  • 41
  • 85
  • Wait… `null` is not a type. That’s nothing that `typeof` would return. – Sebastian Simon Jun 25 '15 at 00:33
  • if mozilla says null is a data type, End of Stroy. anyway there has been several issues related to that. Read this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof – Fawzan Jun 25 '15 at 04:04
  • @Fawzan But type null is categorized as "Object" – tech_boy Jan 18 '19 at 14:19
2

There are 6 basic data types in JavaScript:

  1. A number
  2. A string
  3. A boolean (logical type)
  4. The “null” value
  5. The “undefined” value
  6. Objects and Symbols

For more detail you can go through this link - https://javascript.info/types

Gaurav Gupta
  • 478
  • 6
  • 10
1

The latest ECMAScript standard defines eight data types, namely: Seven data types that are primitives: Boolean, Null, Undefined, Number, BigInt, String, Symbol and Object For more information, refer: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#BigInt_type

jagadish
  • 11
  • 2