js-Data type conversion and data operator detailed explanation

First, the conversion of data types

  1. Why convert?

    • If the user gave a data, or the computer gave a data, or other programs gave a data, not the type of data we need

    • Need to convert to the data type required by the current program, and then perform the operation

    • What are the conversion methods

  2. Data type conversion method

    • Forced conversion (display conversion, active conversion)

      • Character to value

        • parseInt(要转换的数据或变量)

          • Detect from left to right in turn, when the first non-numeric character is encountered, stop conversion

          • Ignore everything after the decimal point, in fact, the decimal point is recognized as a non-digit

          • If the first digit is a non-number, directly NaN

            • What is NaN? not a number

            • Numeric data that is not a number

            • The result of an illegal numerical operation

              • Illegal: Illegal, non-compliant, could not be transferred, must be transferred

        • parseFloat(要转换的数据或变量)

          • Detect from left to right in turn, when the first non-numeric character is encountered, stop conversion

          • Decimal point can be recognized

          • If the first digit is a non-number, directly NaN

            • What is NaN? not a number

            • Numeric data that is not a number

            • The result of an illegal numerical operation

              • Illegal: Illegal, non-compliant, could not be transferred, must be transferred

        • Math.round(要转换的数据或变量)

          • Math.round is not specifically for character to numeric conversion

          • Specially do rounding, take the nearest integer, and then give it to convert

            • Strict conversion: as long as there is a non-number, then it is NaN

        • Number(要转换的数据或变量)

          • Number is not specifically for character to value

          • The built-in constructor of the system is used to create numeric objects.

            • Strict conversion: as long as there is a non-number, then it is NaN

        • how to choose?

          • Select on demand

            • When conversion is required, observe the current running status of the program and the actual situation of the data to be converted, corresponding to the actual rules, and choose the method

            • No matter which one you choose, you can use it flexibly as long as you pay attention to the rules

      • Numeric to character

        • 数值变量.toString()

          • Direct conversion, equivalent to adding a quotation mark

        • 数值变量.toFixed(n)

          • You can specify to keep n as a decimal

Second, the data operator

  

  1. Arithmetic operator

    • + Plus sign

      • When both sides of the plus sign are numeric data, it is a mathematical addition operation

      • If any side is a character, then it becomes a splicing. The splicing is to directly combine the two data.

    • -*/%

      • Is a normal mathematical operation

      • Even if the two sides are not numerical, they will be converted to numerical values ​​by default to perform calculations

      • If a certain data cannot be converted into a value, you will getNaN

  2. Relational operator-the result is a boolean

    • ><>=<=

      • As long as there is a value on one side, it is the comparison rule of the value

      • If they are all characters, it is the character comparison rule : compare bit by bit , get the result, stop

    • !===

      • There will be implicit type conversion, or only compare the size, not compare types

    • !=====

      • No implicit type conversion occurs, not only comparing size, but also comparing types

  3. Logical operator-main operation Boolean value

    • or

      • ||: As long as there is true on both sides, the result is true, must be false, the result is false

    • And

      • &&: As long as there is false on both sides, the result is false, must be true, the result is true

    • non-

      • !:Negate

  4. Assignment operator

    • =

      • Save the data on the right side of the equal sign in the variable on the left

      • Put the content on the right in the variable on the left

      • If the variable on the left has a value, it will be overwritten

    • +=,-=,*=,/=,%=

      • Calculate first, then assign (overwrite)

      • Please refer to:+ - * / %

  5. Unary operator

    • Self-increasing

      • ++

      • Increase by 1

      • Before and after

        • Self-increasing before ++n: calculated first, then used

        • Later increment n++: use first, then calculate

    • Decrement

      • --

      • Decrease by 1

      • Before and after

        • Front decrement --n: calculate first, then use

        • After decrement n--: use first, then calculate

 

Guess you like

Origin www.cnblogs.com/piaoyi1997/p/12749631.html