Most developers believe that dynamic languages like JavaScript are typeless

In fact, ECMAScript types are subdivided into language types and specification types

All values in the ECMAScript language have a corresponding language type

Undefined Null Boolean String Number Object

For JavaScript, we can define “types” : To the language engine and developers, a type is an internal feature of a value that defines the behavior of a value to distinguish it from other values

With a thorough understanding of JavaScript types, we aim to change our preconceptions about casting, see its benefits and realize that its drawbacks have been greatly exaggerated

Built-in types

JavaScript has seven built-in types:
  • Null null
  • Undefined undefined
  • Boolean value Boolean
  • Digital number
  • String string
  • Object the object
  • Symbolic symbol

All but objects are collectively referred to as primitive types

The Typeof operator looks at the worth type, which returns the string value of the type

typeof undefined= = ="undefined"
typeof true= = ="boolean"
typeof 42= = ="number"
typeof "42"= = ="string"
typeof { life: 42} = = ="object"
typeof Symbol() = = ="symbol"

typeof null= = ="object"

// We need to use compound conditions to detect null worth types

var a = null(! a &&typeof a === "object")

typeof funcrion a()= = = {}"function"

typeof [1.2.3= = ="object" 
Copy the code

Function is actually a subtype of Object. Specifically, a function is a callable object that has an internal property [[Call]] that allows it to be called

function a (b, c) {}// The length attribute of a function object is the number of arguments it declares
a.length === 2
Copy the code

An array is also a subtype of Object. The elements of an array are indexed numerically, rather than by string keys like ordinary objects, and the length attribute is the number of elements

Values and types

Variables in JavaScript have no type, only values, and can hold any type of value at any time

To put it another way, JavaScript does not do type enforcement, and the language engine does not require variables to always hold values of the same type as their initial values

So, when a typeof operation is performed on a variable, the result is not the typeof the variable, but the worthy type that the variable holds, because variables in JavaScript have no type

Undefined and undeclared

Variables are undefined when they do not hold values

var a
typeof a // undefined

var b = 42
var c 

b = c

typeof b // undefined
typeof c // undefined
Copy the code

Variables declared in scope but not yet assigned are undefined, whereas variables not declared in scope are undeclared

var a;
a; // undefined
b; // ReferenceError: b is not defined
Copy the code

Typeof returns undefined for undeclared variables because typeof has a special security mechanism

var a;
typeof a; // "undefined"
typeof b; // "undefined"
Copy the code
if (DEBUG) {
    console.log("Debugging is starting")}if (typeofDEBUG ! = ="undefined") {
    console.log("Debugging is starting")}Copy the code