JavaScript interview question summary series (1)

JavaScript data type

Speaking of interviews, technical people should have a feeling that everything you use at work can be checked, but when someone asks a question directly, you can answer it immediately without inquiring. , There is still a certain degree of difficulty. Especially during interviews, many programmers are not lacking in abilities, but it is easy to get stuck when you ask you to speak out immediately or write code on the spot. The author has a similar experience, so I decided to write a summary of interview questions and thoughts on the interview starting from today. Welcome colleagues who are interested in interviews to discuss and make progress together.

1. Data types in JavaScript

There are two types of data types in JavaScript: one is the basic data type, and the other is the reference data type.

  • The basic data types are: Undefined, Null, String, Number, Boolean,Symbol
  • The reference data types are:Object

2. The storage location of the data type

  • Basic data types of the data stored in the stack (Stack) , the reference data type of data stored in the stack (Heap) on.

  • The basic data type (also called primitive data type) is a simple data segment that is directly stored in the stack. It occupies a small space and has a fixed size. It is frequently used data, so it is stored in the stack;

  • The reference data type occupies a large space and is not fixed in size. If stored in the stack, it will affect the performance of the program. The reference data type stores a pointer in the stack, which points to the starting address of the entity in the heap. When the interpreter looks for a reference value, it first retrieves the address in the stack, and then obtains the entity from the heap after obtaining the address.

3. How to judge the data type?

The common methods for judging the data type are as follows, let's talk about them one by one below:

// typeof xxx;

  • Using the typeofmethod of determining the data type, the elementary data type common in the determination mentioned above. And when typeofthe time reference data to determine the type, typeofonly to return functionand objecttwo types of data, this is clearly not a more accurate judgment of the type we want. Therefore, it typeofcan be used when judging the basic data type ; it is typeofnot recommended when judging the reference data type ;
  • typeofFor primitive types, in addition nullit can display the correct type ( typeofdetermination nulltime, returns object);
  • typeofFor objects, except functions will be displayed object;
  • For null, it typewill return object, this is a bug that has existed for a long time.

// A instanceof B;
instanceof commonly used to judge Awhether the Bexamples, if Aa Binstances, then A instanceof Breturned true; otherwise, return false.

instanceofThe principle or internal mechanism is: by judging whether the type can be found in the prototype chain of the object prototype.

E.g:

     var a = {
    
    }; 
     a instanceof Object; // true

     var b = [];
     b instanceof Array; // true

When I got here, everything seemed normal and there was no problem. So, what if Kong Yiji’s idea of ​​"four replies" is brought into play? For example:

    var c = {
    
    };
    c instanceof Array;  // false
    var d = [];
    d instanceof Object; // true

This time, find an array using instanceoftime, both belonging to the circumstances of the array, there are cases belonging to the object. Although we can only use A instanceof Arrayto judge Ain the end is not an array, but when the array can also be instanceof Objectas truewhen, psychological more or less that is not enough simply agile. So continue to see if there is a more efficient way to determine the type of reference data? The answer is yes. please look below:

// Object.prototype.toString.call( xxx );
where "xxx" is the data type we want to judge.

Here toString()is the Objectprototype method, after calling the method, returns the current object [[class]]attribute, which is an internal attribute, the format [object Xxx]in which "Xxx"that type of object.

For the Objectpurposes of the object, direct call toString()method to return [object Object]; while for other objects, by invoking the need
call() / apply()to return the correct type of information methods. such as:

Object.prototype.toString.call('') ;                    // [object String]
Object.prototype.toString.call(1) ;                     // [object Number]
Object.prototype.toString.call(true) ;                  // [object Boolean]
Object.prototype.toString.call(Symbol());               //[object Symbol]
Object.prototype.toString.call(undefined) ;             // [object Undefined]
Object.prototype.toString.call(null) ;                  // [object Null]
Object.prototype.toString.call(new Function()) ;        // [object Function]
Object.prototype.toString.call(new Date()) ;            // [object Date]
Object.prototype.toString.call([]) ;                    // [object Array]
Object.prototype.toString.call(new RegExp()) ;          // [object RegExp]
Object.prototype.toString.call(new Error()) ;           // [object Error]
Object.prototype.toString.call(document) ;              // [object HTMLDocument]
Object.prototype.toString.call(window) ;                //[object global] window 是全局对象global的引用                                                               

4. What is the difference between Null and Undefined?

  • Undefined: Indicates that this value does not exist, "missing value", that is, there should be a value here, but it has not been defined yet. When trying to read it, return undefined.

  • Null: Indicates that an object is defined, the value is "empty value", it is an object, but an empty object, without attributes and methods. In the verification Nulltime, be sure to use ===as ==unable to distinguish nulland undefined.

5. What is the difference between == and ===?

  • ==
    1. For ==instance, if the two sides of the same type, then directly than the size; if the parties are not the same type, it would be the type of conversion.
    2. It first determines whether the contrast nulland undefinedis then returned true;
    3 determines whether or not both are stringand number, if the string is converted Number;
    4. wherein determining whether one Boolean, it will be booleanconverted to digital and then compare;
    5 . wherein determining whether one object, and the other is string, numberor symbolis it the objectturn then determines the type of the original.

  • ===
    Directly judge whether the two types and values ​​are the same.

6. Type conversion

  • Turn Boolean
    when the condition determination, in addition to undefined, null, false, NaN, "", 0, -0, all values are converted to the other true, including all the objects.
  • Object to basic type When an
    object is converted to a basic type, it will be called first valueOfand then called toString, and these two methods can also be overridden.
  • Four arithmetic operators:
    1. Only when the addition operation is used, one of which is a string type, it will convert the other to a string type;
    2. For other operations, as long as one of the operations is a number, then the other is converted to a number;
    3. The addition operation will start three types of conversion: convert the value to the original value, convert to a number, and convert to a string.

(End of this section)


Reference link :
https://www.cnblogs.com/yan-yubo/p/Javascript-built_in_objects.html
https://www.cnblogs.com/M-right/p/9474307.html
https://blog.csdn .net/m0_37686205/article/details/89194248
https://www.cnblogs.com/echolun/p/7889848.html

Guess you like

Origin blog.csdn.net/ZxxSteven/article/details/102672217