Implicit conversion problem in js operations

Implicit conversion problem in js operations

At the beginning of the article, we might as well think about three questions:
1. What is implicit conversion?
2. Why and under what circumstances does implicit conversion occur?
3. What happens to various data types during implicit conversion ? Conversion?
In the following articles we will solve these problems one by one:

1.What is implicit conversion?

I believe we all know the five basic data types in js: Number.String.Boolern.undefined.Null. For example:

  var str = '12'
  var a = 5
  str = String(str) //12
  var b = a*str
  console.log(b)    //60

When this happens, we usually use Number() to convert the string into number type before participating in the operation, but have you ever thought about what will happen when we do not convert str into number type and directly participate in the operation? As a result, what answer will you get?

  var str = '12'
  var a = 5
  var b = a*str
  console.log(b)    //60

I have verified the code, and the result is that the correct answer can still be obtained, and the answer type is number >Conclusion: After the above argument, we can conclude that in the presence of operators, implicit data will occur between two variables of different data types. Transformation, the so-called implicit transformation means that its transformation process is invisible to us.

2.Why and under what circumstances does implicit conversion occur?

After our first round of verification, combined with the execution mechanism of js, we can also draw conclusions:

Because js code is executed from top to bottom, because the two data types usually cannot be operated on, implicit conversion will occur in order to enable the code to execute smoothly.

3. How will various data types be converted in implicit conversion?

Next, let’s take a look at what happens to various data types during the implicit conversion process.
1.Number

  1. String type to Number. Pure numeric string or 0 can be converted to Number type, and the rest are converted to NaN;
  2. Bollean type to Number true–>1 false–>0
  3. Convert undefined type to Number NaN
  4. Convert Null type to NUmber 0

2.String

When the string type encounters the connection operator, implicit conversion to the string will occur. When "+" exists in the operation, as long as there is a string, regardless of whether it is before + or after +, other types will be converted into characters. string type

三.Boolean

When encountering comparison operators or individual relational operators, it will be converted to Boolean type.

Four.undefined

undefined will convert undefined to NaN, and then perform addition and subtraction operations.

5. Null

It will be converted to 0 first before addition and subtraction operations are performed.

6. Objects and Arrays

Arrays and strings are reference data types, and are generally implicitly converted twice. The first time is converted to the string type, and the second time is implicitly converted according to the situation and then involved in the operation.

Guess you like

Origin blog.csdn.net/qq_52648305/article/details/124134023