Totally original! How tired the code word is! Write it down for yourself.

Implicit conversions are not familiar with implicit conversions. Implicit conversions are not familiar with implicit conversions.

Juejin. Cn/post / 684490…


To begin with, explain why the following result returns a value of ’10’. Read the full text, I believe you can answer.

[+ + + [[]] []] + [+ []]Copy the code

As long as the front direction of the students, should have seen this JS author’s emojis:

In the following paragraphs, I will look at how this works.

Not very detailed explanation of the knowledge points, but the design to which say which. Most of this is a JS implicit conversion problem. For those who are not familiar with this section, we recommend an in-stack search: JS implicit conversion.

All right, here we go.

  1. Typeof NaN returns number, as specified in ECMAScript and IEEE 754 floating-point arithmetic standards.

    # 4.4.27 NaN
    Number value that is an “Not-a-Number” value
    Copy the code

    Tc39.es/ECMA262 /# SE…

  2. 99999999999999 How many nines? Don’t count it, I’ll count it for you. It’s a 16-bit decimal system. Why does a 16-bit 9999999999999999 return a 17-bit 10000000000000000? This involves the maximum integer representation range of JS, that is, the operation within this range can ensure accuracy. According to IEEE754, the maximum safe integer for JS is 2^ 53-1, also known as number. MAX_SAFE_INTEGER. 2^53-1 evaluates to 9007199254740991, which is a 16-bit decimal number, and a 16-bit 9… Nine is clearly beyond that range. Those who talk about calculation beyond the range of numerical representation are rascals!

    // JS maximum safe integer
    Number.MAX_SAFE_INTEGER === 2**53 - 1
    Copy the code
  3. 0.1+0.2==0.3 returns false. This is a classic floating point precision problem. A lot of articles have talked about this, and it’s often tested in interviews, so I’ll just say a few words. Inside the computer in binary digital storage, JS, in addition, the figures will be converted into a binary calculation again, and, 0.1 and 0.2 of the binary equivalent of an infinite decimals, taken together, computers can only be accurate to one, and then give up at the back of the digits, and then again after the number of the two binary in addition to give up, Instead of 0.3, it would be 0.30000000000000004.

  4. Math.max() for the untrained eye, the maximum value returns an infinitesimal? But for JS students, math.max () takes an array and returns the largest number in that array. So why return -infinity when the argument is empty? It may seem confusing, but the maximum returns an infinitesimal. This is also the underlying design of JS, which is to say that it was specified when the language was written. It actually makes sense when you think about it. For example, if we want a maximum value, we usually set a variable _max, iterate through the sequence of numbers, and replace _max with anything larger than _max. So how do you guarantee that the following numbers are greater than _max when first compared to _max? It occurs to me that if _max is an infinite value, it must be replaced in the future. Math.min() returns Infinity for the same reason.

  5. []+[] Returns an empty string when you add two empty arrays. Just listen to me break it down.

    In JS, the addition of two objects triggers an implicit conversion.

    This implicit conversion must have rules, and the implicit conversion rule for an object is the ToPrimitive rule. Here do not expand said ToPrimitive rules, do not understand their own station search. For now, all you need to know is that the two objects are implicitly converted according to this rule when performing the + addition operation (known as concatenation).

    ToPrimitive rule: then the object is forcibly converted to the original type. First, it looks up whether the object itself has a valueOf method. If so, the method is executed. If not, the toString method is executed (in some cases, for example, the Date object executes toString first).

    For an empty array [], execute [].valueof(). The result is still [], not a primitive type. Then execute [].toString(). The result will return “”. So the result is pretty obvious. Add two empty strings and the result is “”.

    Many of the following analysis will use implicit conversion and ToPrimitive conversion rules, do not understand you can search.

  6. []+{} The calculated result is “[object object]”. The analysis steps are similar to the previous one: ValueOf () is executed first (if any). ValueOf () of an empty object still returns {}. ToString () is executed. The result of converting an ordinary object to a string is “[object object]”. After the above analysis, [] is implicitly converted to “”, {} is converted to “[object object]”, string concatenation, result is “[object object]”.

  7. {}+[] this is going to be 0, right? At this point you must want to ridicule JS is what garbage language. For {} at the beginning of a line of code, JS can be resolved as an empty object, or as a code block, which is empty and does nothing. This simplifies the expression to +[]. The key point is that the + operator, when used as a binary operator, means addition or concatenation, and when used as a unary operator, means positive, such as +0. It can also convert a non-integer type to an integer, equivalent to the Number() method. The unary operator + triggers the ToNumber implicit conversion. Now look at +[].

    The ToNumber rule states that the ToPrimitive rule first converts an object or array to a numeric type.

    We have already analyzed the ToPrimitive conversion to an empty array [], which results in an empty string “”. What is the result of the ToNumber conversion of the empty string (i.e., Number(“”))? According to ToNumber’s rule, the result is 0, which is the final result.

  8. The result of true+true+true is 3. We are now becoming familiar with this process of analysis. Non-numeric primitive types are added, and JS has set the corresponding conversion rules (here we mainly talk about numbers, Boolean, string, NULL, undefined these five categories). :

    1. I am, therefore, between the Boolean, numeric, NULL and undiNumber(undefined)NaN)
    2. A string is added to other types, converted to a string, and then concatenated

    If true+true+true meets the first rule, all the values are converted to numeric types: 1+1+1===3. Null +true===1 null+ 123 ===null123

  9. True-true The result is 0. Subtraction is involved here. JS has special rules for subtracting non-numeric primitives (numbers, Boolean, string, null, undefined) :

    For subtracting between non-numeric primitive types, just remember to convert all to numeric types first, and then subtract. For example, ‘100’-true === 100 -1 === 99 Note that Number(null)===0 and Number(undefine) is NaN.

    True-true => All values are converted to values: 1-1 === = 0

  10. True ==1 Results in true. Similarly, an implicit conversion is triggered when equal is compared with ==.

    1. Boolean type and other types of equality comparison,Boolean first converts to numeric type
    2. Equality comparison of numeric and string types,The string type is converted to a number first
    3. Equality comparison of object type and primitive type,The object type will followToPrimitiveThe rule is converted to the original type
    4. nullundfinedCompare with other values
      • nullundfinedAnd equal to itself= =, and other values are not equal. (ECMAScript states that conversions are no longer triggered)
      • undfinednullAnd equal to itself= =, and other values are not equal.

    true==1 => 1==1 => true

  11. True === 1 results in false. The full equals sign === has no implicit conversion and directly compares types and values.

  12. (! + + [] [] +! []).length results in 9. Let’s follow ToPrimitive’s rules step by step. Take the inverse operator! And the bullet operator + high priority, operation first. Results:

    // +[] = 0,! If [] is false =>! 0 + [] + false => true + [] + false => true + '' + false => 'truefalse'Copy the code

    Finally, the result is a string ‘truefalse’ of length 9.

  13. For 9+”1″ and “91”-1, see rules 8 and 9 above.

  14. []==0 results in true. This case is a == comparison between the object and the primitive type. [] Perform ToPrimitive implicit conversion, resulting in “” Now compare primitive types: “”==0. By rule: When a number is compared to a string, an attempt is made to convert the string to a numeric value. “”==0 => 0==0 => true

    Loose equals comparison == rule: MDN ==

See, what seems unreasonable, you just don’t understand it.

Welcome error correction!!