Data type classification

In JavaScript, each value has a corresponding data type. There are currently eight data types in JavaScript, which can be divided into two broad categories: primitive types and complex types:

  • Primitive type:

    1. UndefinedThere is only one special valueundefinedIs not defined or does not exist.
    2. NullThere is only one special valuenull, indicating a null value.
    3. BooleanA Boolean type, which represents a Boolean value, has two values:true,false.
    4. StringA string that represents textual data, such as'JavaScript'.
    5. NumberA numeric type that uses a 64-bit double precision floating-point type to represent an integer or a floating point number, for example25,3.14.
    6. BigIntA numeric type that can represent an integer in any precision format.
    7. SymbolRepresents a unique identifier.
  • Complex types:

    1. ObjectRepresents an unordered set of key – value pairs.

The typeof operator

To determine the typeof the value of a variable in JavaScript, use the typeof operator, which returns a string representing the (unevaluated) data typeof the operand as follows:

typeof operand
typeof (operand)
Copy the code

The possible return values of Typeof are as follows:

type The results of
Undefined ‘undefined’
Null ‘object’
Boolean ‘boolean’
String ‘string’
Number ‘number’
BigInt ‘bigint’
Symbol ‘symbol’
Object ‘object’
function ‘function’
An array of ‘object’
Other objects ‘object’

The following is an example of typeof:

// Undefined
let a;
typeof a; // 'undefined'

// Null
let b = null;
typeof b; // 'object'

// Boolean
let c1 = true;
typeof c1; // 'boolean'
let c2 = false;
typeof c2; // 'boolean'

// String
let e1 = 'JavaScript';
typeof e1; // 'string'
let e2 = ' ';
typeof e2; // 'string'
let e3 = '123';
typeof e3; // 'string'

// Number
let f1 = 987;
typeof f1; // 'number'
let f2 = 3.14159;
typeof f2; // 'number'

// BigInt
let g = 24n;
typeof g; // 'bigint'

// Symbol
let h = Symbol('foo');
typeof h; // 'symbol'

/ / object
let personObject = {
  name: 'lufei'.age: 18
};
typeof personObject; // 'object'
let array = [1.3.'5'];
typeof array; // 'object'
let date = new Date(a);typeof date; // 'object'
let regex = /\d/;
typeof regex; // 'object'
typeof Math; // 'object'

/ / function
function fn1() {}
typeof fn1; // 'function'
const fn2 = function xxx() {};
typeof fn2; // 'function'
Copy the code

Undefinedtype

There is only one value in the Undefined type — the special value Undefined, which means Undefined, is where there should be a value, but it’s not defined. The value undefined can be obtained in the following scenarios:

  • The variable is declared but not assigned.

    let a;
    a; // undefined
    Copy the code
  • Object has no assigned property.

    let obj = new Object(a); obj.name;// undefined
    Copy the code
  • A function that returns no value returns undefined by default.

    function fn() {}
    fn(); // undfined
    Copy the code
  • When the function is called, the argument that should be provided is not provided, then the argument is undefined.

    function fn(a) {
      console.log(a)
    }
    fn(); // undefined
    Copy the code
  • Using the Typeof operator on an undefined variable will also result in undefined.

    // The variable foo is undefined
    typeof foo; // undefined
    Copy the code

Of course, you can also explicitly assign undefined to the variable:

let a = undefined;
a === undefined; // true
Copy the code

But this is unnecessary and unreasonable, because by default, any uninitialized variable gets the special value undefined. In general, never explicitly assign undefined to a variable.

Nulltype

The Null type also has only one value — the special value Null, which represents an empty object pointer, that is, already defined, just Null. For example, when declaring a variable that will hold the value of an object, we can initialize it with null:

let query = null;
Copy the code

When using the equality operator, JavaScript assumes that undefined and null are ostensibly equal:

undefined= =null; // true
Copy the code

But there are some differences:

  • Undefined means undefined at all; Null means that only null values are defined. So to determine whether a value exists, use undefined:

    name === undefined;
    Copy the code
  • When converted to a value, Number(undefined) is NaN; And Number(null) is 0.

  • It is unreasonable to assign undefined to a variable; It makes sense to assign null to a variable.

  • They are not equal under the strict equality operator.

Booleantype

A Boolean value is a data type whose value can only be true or false. It gives programming languages the ability to logically represent true or false. With this capability, if statements, for loops, and so on can be implemented.

In JavaScript, true and false values of type Boolean are expressed as true and false, respectively, and both are case sensitive. Here is an example of assigning a Boolean value to a variable:

let visible = false;
let loading = true;
Copy the code

Although the Boolean type has only two Boolean values, the values of the other data types all have corresponding Boolean equivalent forms. To convert a value of another type to a Boolean value, use the Boolean() function, which works as follows:

let stringAsBoolean = Boolean('foo');
stringAsBoolean; // true

let numberAsBoolean = Boolean(0);
numberAsBoolean; // false
Copy the code

In the above code, both the string foo and the value 0 are converted by the Boolean() function to the corresponding Booleans: true and false, respectively. So which values will be converted to true and which values will be converted to false? This depends on the following rules:

The values undefined, NULL, ” (empty string), 0, NaN, and false are converted to false, and the rest of the values are converted to true. Consider the following example:

Boolean(undefined); // false
Boolean(null); // false
Boolean(' '); // false
Boolean(0); // false
Boolean(NaN); // false
Boolean(false); // false

Boolean('aaa'); // true
Boolean(' '); // this is a string that contains Spaces, not an empty string
Boolean(123); // true
let obj = {};
Boolean(obj); // true
let array = [];
Boolean(array); // true
function fn() {}
Boolean(fn); // true
Copy the code

Also, in JavaScript, any position that is expected to be a Boolean value is automatically converted to the corresponding Boolean value, using the same rule as above. For example, in the if flow control statement:

let a = 'foo';
if (a) {
  console.log('A is automatically converted to true');
}
Copy the code

Running the above code, the console will say that a is automatically converted to true. This is because the criterion following if is expected to be a Boolean, so the string a is automatically converted to the Boolean true, executing the code inside the code block.