How to answer JS data type interview questions

Refer to the authoritative "JavaScript Advanced Programming (3rd Edition)" book to answer all aspects: part of the self-test code

The number of types is 6 : There are 5 simple data types (also called basic data types) in ECMAScript : Undefined , Null , Boolean , Number
And String . There . 1 Species complex data types - Object , Object essentially by a set of name-value pairs of unordered. ECMAScript
No mechanism for creating custom types is supported, and all values ​​will eventually be one of the above 6 data types. At first glance, it seems that there are only 6
This data type is not enough to represent all data; however, due to the dynamic nature of the ECMAScript data type, it is indeed not defined
Other data types are necessary.
 
What does the typeof function return: self-test returns function on chrome version 84.0.4147.89
 
var f=()=>2;
//undefined
typeof f
//"function"

var message = "some string"; 
alert(typeof message); // "string"
alert(typeof(message)); // "string"
alert(typeof 95); // "number"
These few examples illustrate that the operand of the typeof operator can be a variable ( message ) or a numeric literal. note,
typeof is an operator rather than a function, so although the parentheses in the example can be used, they are not required.
Sometimes, the typeof operator returns some confusing but technically correct values. For example, call typeof null
Will return "object" because the special value null is considered an empty object reference. Safari 5 and earlier, Chrome 7 and later
The previous version will return "function" when calling the typeof operator on a regular expression , while other browsers will return in this case
"object"
From a technical point of view, a function is an object in ECMAScript , not a data type. However, the function also
There are indeed some special attributes, so it is necessary to distinguish functions from other objects through the typeof operator.
 

Guess you like

Origin blog.csdn.net/taozi550185271/article/details/107700576