“This is the fifth day of my participation in the First Challenge 2022. For details: First Challenge 2022”

Written on the front

Most junior partner see technical books would use “on” to describe the intuitive feeling of reading, of course, I also am a front-end small white, white transparent kind, but I read technical books felt was “eating”, I would like my face little red book in the fourth edition of the idea of the process, summarize to everybody, for newcomers can easily.

Note: this article is due to the author’s level, if there are mistakes, please correct, in addition, with the deepening of learning, experience, I will keep coming back to update, modify this kind of article.

Mind mapping

vein

This section provides an overview of 3.4 Data types, the Typeof operator, and the undefined data type.

3.4 Data Types

An overview of the

Simple data types

Alias: Primitive type

Undefined, Null, Boolean, Number, String and Symbol.

Complex data types

Simply put, an Object contains many properties and methods. It’s a total set

Usage: Data types in JavaScript are “live”; one data type can be used as another data type. So it’s more arbitrary.

3.4.1. Typeof operator

  1. Determine the data type of any variable

The first step in determining the six variable data types is to know what they represent. The book has already written about the meanings of the six data types.

To use typeof is to append the variable name directly after typeof. The value returned is the corresponding data type of the variable.

const age = 100;

console.log(typeof age); // The output is Number
Copy the code
  1. Distinguish between functions and other objects

A function is a special kind of object that has directly unique properties. So you have to distinguish it from objects, functions are not data types!

3.4.2 Undefined type

  1. There is only one value, initialize the default assignment

When you declare a variable without assigning a value to it, the default value of the variable is undefined.

var age;
var age; // No error will be reported
Copy the code
  1. Literals undefined

The variable is declared, but not assigned.

Purpose: Undefined was not used before, but to distinguish it from null null object Pointers. That’s how we derived the data type variable undefined.

  1. The difference between a variable containing undefined and an undefined variable

(1) the console log

Using console.log for declaring a variable will output undefined, but using console.log for declaring a variable will generate an error indicating that the variable is not declared.

(2) the typeof

The core reason for initializing variables is to use Typeof to distinguish whether a variable has been declared or not.

Reason: The Typeof operator returns undefined for undeclared variables as well as declared but unassigned variables. So there’s no way to distinguish it by typeof. So we have to make them different before we use typeof, so think about how to make them different, by the way, by assigning values to declared variables, so that typeof returns are no longer undefined, one result is undefined, one result is not, and of course we know whether the variable is declared or not.

Essential differences in return values

Declare variables unassigned and undeclared variables although typeof both returns undefined. This is logically correct, because while strictly speaking, these two variables are fundamentally different, neither of them can actually perform operations. (From the Book)

Note: only judging undefined is actually not rigorous, because there are many cases of false values.

(4) the delete

Calling delete on an undeclared variable also does not return an error, but this operation does not help and will in fact throw an error in strict mode.

⑤ The try-catch distinction is undeclared and uninitialized

To tell whether a value is undeclared or uninitialized, use try-catch, for example:

if (typeof age == 'undefined') {

    try {

        // Not initialized

        console.log(age);

    } catch (error) {

    console.error('age is undeclared.'); }}Copy the code

⑥ If checks that undefined is false

Undefined is a false value, what is a false value, that is, after the data type conversion, will be converted to false. Since it can be converted to a Boolean value false, it is natural to use the conditional statement to determine whether it is a false value, and therefore whether it is undefined.