1. Introduction

  • Is it dross orfeature? It depends on how you recognize and use it. If it is used well, it is a feature. If it is not used well, it may not only be dross, but also be required to work overtime in the company. So in order to take advantage of implicit conversions, I have to know them well.
  • And most of the reason for the confusion of JS, but also because of its want to static language uncertainty.

Thanks for inventing JavaScript!!!!

2. 0.1 + 0.2 == 0.3

Analysis of the

  • The output is false

  • This is an old problem, not only in Javascript, but also in other languages. The numbers seen in high-level languages are implicitly converted to binary for computation. The problem is the error in the conversion to binary.

    1. In simple words, in the calculation process of 0.1 + 0.2, after two error calculation. The first time, after the first conversion to a double precision binary floating-point number, the binary floating-point decimal can only store 52 bits, resulting in a trade-off between the number after 52 bits.
    2. The second time, the addition of 0.1 and 0.2, which had been converted to binary, occurred outside the range of digits.
  • The two errors result in 0.1 + 0.2 == 0.3 being false.

In business development, it usually affects the calculation of price. So to get around this problem, what comes back in the background is usually multiplied by 100, which is always an integer.

The solution

  • This is already the case, but if you still encounter this application scenario. It is not impossible to solve.\
    1. Multiply it by 1,000 and get rid of it

      (0.1 * 1000 + 0.2 * 1000) /1000= =0.3
      Copy the code
    2. Use the number. EPSILON mode of ES6

      console.log( Math.abs(0.1 + 0.2 - 0.3) < =Number.EPSILON);
      Copy the code
    3. Use math.js as a library, I heard that some numbers are still a problem, accurate calculation or to the background bar 🤔

    4. Use math. round and Math.pow methods combined

Typeof easy to forget three points

  • Typeof [[target]] returns a string

  • typeof null

    console.log(typeof null) // 'object'
    Copy the code

    It’s the same thing as it is now, just remember, it’s a pit

  • typeof NaN

    // 'number'
    Copy the code
  • The full name for NaN is not a number, but typeof comes out as number.

  • It’s too blind, and it gets worse

    NaN == Nan // false
    Number.Nan == NaN // false
    NaN= = =NaN // fasle
    Number.NaN === NaN // false
    
    Object.is(NaN.NaN) // false
    Copy the code
  • You can never be yourself.

4. 9999999999999999

console.log(9999999999999999) / / 10000000000000000
Copy the code
  • Or js accuracy w problem, more than 53 bits no accuracy

5. Implicit transformation

5.1 adds up and decreases

 1+ {} = = =? {} +1= = =?1+ [] = = =?1 + '2'= = =?Copy the code
  • 1[Object object], [object object]1, 1, 12

  • Null Object is the Object. The prototype. ToString. Call ({})

  • An empty array is converted to 0

1 + '1'= >11
1 - '1'= >0
Copy the code
  • So the result here is that the string hits the + sign, you touch the string with the two, and then you connect them
  • But if it’s a – sign, convert the string to Number and subtract
const a = []
if (a) {
    console.log('1')}if (a == true) {
    console.log('2')}Copy the code
  • Result: prints 1
  • For a single empty array, typeof a === ‘object’, so 1 can be printed
  • Number(a) == Number(ture) => 0 == 1 => false So the 2 is not printed
    console.log(new String('abc') = =true)
    console.log({} == ture) 
    console.log([] = ! [])Copy the code
  • Result fasle, false, true
  • Is there anything different from what you think, before I have no in-depth summary of JS implicit conversion, I have seen every time will be wrong. It’s called guessing, and it’s uncomfortable to feel out of control.

5.2 Messy transformation

  • In the example above, none of the normal operations were performed in some way in the back. Sure enough, it took ten days to make something simple. It is inevitable that TS will prevail now. 😢

  • Please, all teams use TS!

  • Why would you want to run different kinds of things on a project?

  • But when you’re in an interview, try to find a pattern by using brute force

Regular +.

```js // number, string, boolean, null, undefined, Exclude symbol and bigInt console.log(1 + '1') // '11' console.log(1 + true) // 2 console.log(1 + 1) // 2 console.log(1 + null) // 1 console.log(1 + undefined) // NaN console.log(1 + []) // 1 console.log(1 + {}) // 1[object object] // Character string console.log('1' + '1') // '11' console.log('1' + true) // '1true' console.log('1' + null) // '1null' console.log('1' + undefined) // '1undefined' console.log('1' + []) // '1' console.log('1' + {}) // '1[object Object]' // boolean console.log(true + true) // 2 console.log(true + null) // 1 console.log(true + undefined) // NaN console.log(true + []) // true console.log(true + {}) // true[object Object] // null console.log(null + null) // 0 console.log(null + undefined) // NaN console.log(null + []) // null console.log(null + {}) // null[object object] // console.log(undefined + undefined) // NaN console.log({} + {}) // '[object Object][object Object]' console.log({} + []) // '[object Object]' console.log([] + []) // empty ```Copy the code

Here are some rules:

  1. An empty array is represented in the plus sign as empty, which is empty, and when you add it together it returns whatever it is, including its type.
  2. If you add strings to both sides of the plus sign, you will add the sum to the string first, which is consistent with the first point. Empty arrays are reserved for empty strings
  3. And the second thing, if you’re adding objects, it doesn’t matter if they’re empty objects. Will be converted to a string
  4. All other variables are converted to medium numbers first.

– no rules

```js console.log(1 - '1') // 0 console.log(1 - true) // 0 console.log(1 - 1) // 0 console.log(1 - null) // 1 console.log(1 - undefined) // NaN console.log(1 - []) // 1 console.log(1 - {name: 'I have a value of'}) / / NaN. The console log (' -- -- -- -- -- -- -- -- -- -- -- -- -- - ') to the console. The log (' 1 '-' 1 ') / / 0 console. The log (' 1 '- true) / / 0 console. The log (' 1' - null) // 1 console.log('1' - undefined) // NaN console.log('1' - []) // 1 console.log('1' - {}) // NaN console.log('--------------') console.log(true - true) // 0 console.log(true - null) // 1 console.log(true - undefined) // NaN console.log(true - []) // 1 console.log(true - {}) // NaN console.log('--------------') console.log(null - null) // 0 console.log(null - undefined) // NaN console.log(null - []) // 0 console.log(null - {}) // NaN console.log('--------------') console.log(undefined - undefined) // NaN console.log({} - {}) // NaN console.log({} - [])  // NaN console.log([] - []) // 0 ```Copy the code
  • Subtraction is much simpler, just subtract the Number from both sides
  • NaN subtracted from any type is NaN
  • [] – [] is an exception, not NaN, I don’t understand. I could not find the corresponding answer on the Internet or I may have a problem searching the keyword. The problem is as long as it isNumber([]) å’ŒNumber([1, 2])alreadyNumber([1])Up here, why is it so sulky zero NaN one.

! Transformation rules of

```js let b const a = !! b ```Copy the code
  • Double exclamation point enforces type to Boolean

    The data type Conversion of true Conversion to false
    Boolean true false
    Number Any non-zero number, including infinity 0 and NaN
    String Any non-empty string ‘ ‘
    Object Any object null
    undefined undefined

== transformation rule

  • Instead of TS, you should at least use === 😂, why use ==?
  • A direct comparison of the same type, except for reference types
  • Everything else is converted to number on both sides except for the following
  1. Comparison of reference types and numbers/strings

    const a = {}
    console.log(a == 1) // fasle
    console.log(a == '1') // fasle
    Copy the code
    • Do the following in the JBS engine:
      • A. valueof () gets whether it is a basic type
      • If not, toString() is called[object Object]
      • [object Object]Nature and one don’t want to wait.
    const a = new String(123)
    cosnole.log(a == 123) // true
    
    cosnt b = new String('aaa')
    cosnole.log(b == 'aaa') // true
    Copy the code
  • Calling valueOf() directly yields the underlying data type.
  1. Comparison of reference types and reference types

    console.log([] == []) // false
    console.log([] == {}) // false 
    console.log({} == {}) // false 
    Copy the code
  • If both sides are reference types, the direct comparison is to the address to which they point. The address of course cannot be different, unless the following operation
    const a = {}
    const b = a
    console.log(a == b) // true
    Copy the code
  1. Null, undefined, NaN, and String Number Boolean Object When comparing, the result byte of the comparison is false

  2. [] = =! [] true is not a special case

    • This is thought of in conjunction with [[operator priority]].
    • ! The priority of is greater than ==
    • So! [] is false, and [] is a reference type toString() is[object Object].
    • If [] == fasle, both sides are converted to number,
    • 0 == 0, true

Greater-than-less rule

  • There is a classic case for this problem.

    const a = [1.23.12]
    cosnole.log(a.sort()) 43, / / [1, 5]
    Copy the code

    As we all know, this is due to sort and the way it sorts is by turning the numbers in the array into a string and comparing ASCII.

    function sortNumber(pre, next) {
    	return pre - next
    }
    console.log(a.sort(sortNumber())) / / [43] 1, 5,
    Copy the code

    Only by adding such functions yourself can you really achieve the sort you want. These are the two pre and next values that ES gives you. If the return is true, the value in front is greater than the value in the back, and the two are switched, and vice versa. This is the simple utility of [[bubble sort]] in [[data structure]]

    “> < p style =” max-width: 100%; clear: both; min-height: 1em

  • So, the following is false

    console.log('23' > '3')
    Copy the code

6. Wish world peace, wish all the teams to use TS as soon as possible, happy every day