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

后端 未结 5 2021
难免孤独
难免孤独 2021-01-02 16:48

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

  • numbers
  • strings
  • booleans
5条回答
  •  时光说笑
    2021-01-02 17:12

    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
    

提交回复
热议问题