JavaScript data types and typeof operator

JavaScript data types

There are 6 different data types in JavaScript:

  1. string
  2. number
  3. boolean
  4. object
  5. function
  6. Symbol
    3 types of objects:
  7. Object
  8. Date
  9. Array
    2 data types that do not contain any values:
  10. null
  11. undefined

typeof operator

You can use the typeof operator to view the data types of JavaScript variables.
Examples:

typeof "John"                 // 返回 string
typeof 3.14                   // 返回 number
typeof NaN                    // 返回 number
typeof false                  // 返回 boolean
typeof [1,2,3,4]              // 返回 object
typeof {name:'John', age:34}  // 返回 object
typeof new Date()             // 返回 object
typeof function () {}         // 返回 function
typeof myCar                  // 返回 undefined (如果 myCar 没有声明)
typeof null                   // 返回 object

caution:

  1. The data type of NaN is number
  2. The data type of Array is object
  3. The data type of Date is object
  4. The data type of null is object
  5. The data type of undefined variables is undefined

If the object is JavaScript Array or JavaScript Date , we cannot judge their type by typeof, because they all return object .

Guess you like

Origin blog.csdn.net/Serena_tz/article/details/114087584