How many data types are there in JS, and what are they?

I started reading the Javascript book for children. In it, the author claims that there are three types of data:

  • the numbers
  • strings
  • boolean

However, according to W3Schools , there are four:

  • the numbers
  • strings
  • arrays
  • objects

I wanted to know which one is correct.

+6
source share
4 answers

You can test it with the typeof operator:

The typeof operator provides data type names that appear before any single operand.

Therefore, try using typeof with any operand variable: it will give one of the following data type names:

  • Line
  • number
  • Boolean
  • An object
  • Undefined

Therefore, these are 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 
+4
source

Check 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

+2
source

Everything is not as simple as described in the answers above ... they are usually not in javascriptland;)

typeof is the "official" function that is used to get type in javascript, however in some cases this may lead to unexpected results ...

1. Lines

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

"line"

2. Numbers

typeof 42 or
typeof NaN , lol

"number"

3. Bool

typeof true (valid values ​​are true and false )

"logical"

4. Object

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

"an object"

5. Function

typeof function(){}

"function"

6. Undefined

var var1; typeof var1

"undefined"

An alternative is to use ({}).toString() , which will get you a slightly more accurate answer most of the time ...

+2
source

JavaScript has 7 basic data types:

  1. Number
  2. Line
  3. Logical (logical type)
  4. Zero value
  5. Undefined value
  6. Objects and Symbols
  7. Typeof operator

For more information, you can follow this link - https://javascript.info/types

0
source

Source: https://habr.com/ru/post/989701/


All Articles