Writing in the front
The first time I went to an interview, the interviewer asked me 0.1 + 0,2 __ 0.3? Most people know that in JS 0.1 + 0.2! = 0.3, greater than or less than I really don’t know.
There are many abnormal questions like this in JS, so the deer simply sorted into a series, hoping to further strengthen your JS foundation, but also hope that you can get the offer in the interview smoothly.
1. Learning confusion
I first talk about my own confusion in the aspect before, learning a lot of basic knowledge of JS is not so smooth at the beginning, especially when I just contact WITH JS, because JS history left some bugs, plus it is a weakly typed language, unlike other object-oriented strong typed language many concepts as clear.
2. Study suggestions
Null is an object type detected by Typeof. Null is an object type detected by Typeof. When do you understand how it works?
If you’re feeling lazy, don’t study it yet. If you’re interested in how it works, you’ll learn to explore it. , however, when the interview when the interviewer asked you this question, will ask you the recursion, at that time, you have to learn principle, this is you will find that the original rational things scattered, the east, the man said, then I according to the quality of articles under the study, together a series of.
Mind mapping
1. Classification of basic types
The basic type of JS is divided into two types, namely primitive type and object type.
What are primitive types? What are the object types? Why is it classified this way? What’s the difference?
Primitive types store values, whereas object types store addresses. The diagram below:
As shown above, we understand the fundamental difference between primitive types and object types, so the problem arises again. When we assign values between variables, primitive types assign values directly, while object types assign addresses.
So, when we pass an object as a parameter, inside the function we change the object to which the address points, while passing an argument of the primitive type we change a copy (a copy of another value). That is, if it is an object, the original value is changed, but the original value of the original type cannot be changed.
There are six primitive types: String, Number, Boolean, NULL, undefined, symbol.
Next, we will solve the problems in each of the six primitive types.
2, null,
There is a historical bug with null. Is null an object type? Although we use Typeof to detect that null is an object type, this is actually a bug.
2.1 What are the reasons?
Because the original version of JS is a 32-bit system, in order to improve performance, variable type information will be stored in the low level. The beginning of 000 means that represents the object, and null means all zero at this time, so the system will wrongly judge null as the object type. Although the JS internal judgment code has changed, the bug persists.
2.2 Summary of NULL Occurrences
1. Manually set the value of a variable or an attribute of an object to NULL (no value will be assigned later).
If the specified element object is not retrieved, the result is usually null.
Object.prototype._proto_ is also null.
4. If no result is obtained, the default value is null.
3, symbol
Symbol is rarely used, but it exists for a reason.
3.1 What is a symbol?
Symbol represents a unique value, because since the attributes of the object are all strings, we cannot avoid the problem of identical string collisions. So collisions are introduced to prevent objects from having attributes of string type.
3.2 Use of symbol
// The argument is usually a string. If it is an object, the toString method is calledletS1 = Symbol(parameter); // This argument can be thought of as a description of the Symbol instance that distinguishes the first version of the Symbollet a = {};
a[s1] = 'Hello! '; // The second waylet a = {
[s1] = 'Hello';
}
Copy the code
4, the undefined
4.1 What happens to undefined?
1, variable promotion: only undefined default value is undefined.
2, in strict mode: there is no clear execution subject,this is undefined.
3, the object does not have the property name, the property value is undefined.
4, function definition parameters do not pass values, default is undefined.
5. The function returns no value (no return or return;) By default, undefined is returned.
5、为什么 0.1 + 0.2 != 0.3 ?
We talked a little bit about data types. What about 0.1 + 0.2 __ 0.3? Why is it unequal? How should you answer the interviewer?
5.1 What causes this situation?
The reason is very simple, JS uses a double precision version, this version has precision problems, resulting in the above situation.
5.2 What is the internal principle?
All the information of our computer is stored in binary, so the binary of 0.1 represents an infinite repeating decimal. This version of JS adopts floating point number standard, which needs to intercept the binary of this infinite repeating decimal, resulting in the loss of precision, causing 0.1 to be no longer 0.1. 0.1 becomes 0.100 after interception… 001,0.2 becomes 0.200… 002. So the sum is greater than 0.3.
Well, since 0.1 is not equal to 0.1, why did I print console.log(0.1) on the console to be equal to 0.1?
Because when the input is converted, binary is converted to decimal, and then decimal is converted to a string, approximations occur during the conversion process, so what is printed is an approximation.
If you explain the above principle to the interviewer, gosh, it’s hard for the interviewer not to give you an offer, haha, just kidding, getting an offer depends not only on the basics, but also on your other comprehensive abilities.
summary
Today we mainly summed up the JS basic knowledge points, although these knowledge points are relatively pit, also more miscellaneous, but through sorting out a lot of clear, the article may speak of local problems, can be pointed out to the deer.
These are not only the basis, but also the highest frequency interviewers in the interview often asked, different companies interviewers ask the method is also very different, so change the same, the knowledge points summarized above also have a lot of shortcomings, welcome you to supplement.
❤️ Don’t forget to leave your learning footprints[Likes + Favorites + comments]
After reading the article, why not give it a thumbs up? Hee hee, that shows you are very selfish, you are afraid of so good articles let others also see. Just a little joke.
In fact, I am also very selfish, I have always insisted on the original public account: “Deer animation learning programming” secretly to you, which gathered deer in the form of animation to explain data structure and algorithms, network principles, Web and other technical articles.
The author Info:
【 author 】 : Deer
[Original public account] Deer animation learning programming
[Introduction] : I learned programming from the ground up with Lu through animation, and presented the Web front-end field, data structure and algorithm, network principle and other easy-to-understand to my friends. Public reply “information” to send a from zero self-study information package!
[reproduced description] : reproduced please explain the source, thank you for your cooperation! ~