最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How many data types are there in JS, and what are they? - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Jun 25, 2015 at 0:21 Sebastian Simon 19.5k8 gold badges60 silver badges84 bronze badges asked Jun 24, 2015 at 23:55 eagercodereagercoder 5442 gold badges10 silver badges23 bronze badges 1
  • Don't forget Symbol and BigInts. – Константин Ван Commented Nov 6, 2018 at 15:36
Add a comment  | 

5 Answers 5

Reset to default 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

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...

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

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

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

发布评论

评论列表(0)

  1. 暂无评论