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
typeof
method of determining the data type, the elementary data type common in the determination mentioned above. And whentypeof
the time reference data to determine the type,typeof
only to returnfunction
andobject
two types of data, this is clearly not a more accurate judgment of the type we want. Therefore, ittypeof
can be used when judging the basic data type ; it istypeof
not recommended when judging the reference data type ; typeof
For primitive types, in additionnull
it can display the correct type (typeof
determinationnull
time, returnsobject
);typeof
For objects, except functions will be displayedobject
;- For
null
, ittype
will returnobject
, this is a bug that has existed for a long time.
// A instanceof B;
instanceof
commonly used to judge A
whether the B
examples, if A
a B
instances, then A instanceof B
returned true
; otherwise, return false
.
instanceof
The 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 instanceof
time, both belonging to the circumstances of the array, there are cases belonging to the object. Although we can only use A instanceof Array
to judge A
in the end is not an array, but when the array can also be instanceof Object
as true
when, 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 Object
prototype 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 Object
purposes 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, returnundefined
. -
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 verificationNull
time, be sure to use===
as==
unable to distinguishnull
andundefined
.
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 contrastnull
andundefined
is then returnedtrue
;
3 determines whether or not both arestring
andnumber
, if the string is convertedNumber
;
4. wherein determining whether oneBoolean
, it will beboolean
converted to digital and then compare;
5 . wherein determining whether oneobject
, and the other isstring
,number
orsymbol
is it theobject
turn 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 toundefined
,null
,false
,NaN
,""
,0
,-0
, all values are converted to the othertrue
, including all the objects. - Object to basic type When an
object is converted to a basic type, it will be called firstvalueOf
and then calledtoString
, 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