JavaScript’s data type conversion principle (dry goods)

We all know that JavaScript is a weak type (or dynamic type) language, that is, the type of variable is uncertain.

VAR NUM = 123; // 123VAR NUM = ‘Hahaha’ + Num; // “Hahaha123”
In the above code, the variable NUM is initially a value, and later become a string. The variable type is completely determined by the current value. This type is called weak type.

We know that in the programming language, there is a type between the data itself and the operation.

In a strong type of programming language, different types of variables cannot be directly calculated.

However, different types of variables in the weak type language can be added directly, so the type of conversion data is required in the calculation. In most cases, this data type is automatically converted, but sometimes it is necessary to manually enforce the conversion,

Before performing data type conversion, let’s first understand what is the data type of JavaScript.

5 Basic Data Type: Number, String, Boolean, undefined, UNLL.

  • A complex data type: Object.

  • Sometimes we need to know the data type of a variable, can be operated by TypeOf (). The type of return value is: String.

VAR Arr = [undefined, true, ‘world’, 123, null, new object, function () {}] for (i = 0; i

 The result of the output is: undefined, boolean, string, number, object, object, function   null is obviously the basic data type, why the result is Object. This is because Null is considered empty. Object references. Remember. < arr.length; i ++) {
 The function is not a data type, but why does the function type will appear after calling TypeOf. From the perspective of technology, the function is an object. But there are also some special properties, so it is necessary to use TypeOf to distinguish between functions and objects.  
Data type

1 converts non-numerical conversion into numerical types

There are three functions to convert non-numerical values: Number (), PARSEINT (), PARSEFLOAT ().
The first function Number (MIX) can be used for any data type, which first converts the MIX data type to the Number type, and then converts the value of MIX to a value.

If the value of MIX can be converted directly into a number, then it is displayed directly. If it is not possible to display 0 or NaN.

and the other two functions are specifically used to convert the string to a value.
PARSEINT (STRING) function: Convert the string to values, do not follow the rounds. The String here must be the beginning string of the digital type, and the character traversed to the non-value is stopped. If not the beginning of the number, NaN.

VAR NUM = [“123”, “124.4”, “234ASD”, “ASF456” ]; for (i = 0; i


OK results: 123, 124, 234, NaN

parseFloat (string):. The string is converted to floating point. From start to see digital bits, until the end of the non-digits, usage and parseInt (string) is consistent.

parseInt () function has another use.
parseInt (string, radix): In radix as the substrate, the string is converted into decimal integer. radix value of 2-32.
  2, other types of data will be converted to a string type of function  < num.length; i++) {
 There are two types of functions can be converted to the other data string. toString () and string ().  
String (mix): to convert the mix to a string type. This function can convert any value of a string data type.
toString () function in two ways. ,

a Usage: demo.toString (): converts a string type demo. demo not equal to null undefined

Usage two: demo.toString (radix): The decimal numbers into target demo hexadecimal numbers. The 123.0.toString (8) is a decimal number 123 is converted to the octal string.

Note: can not write 123.toString (8) because the browser will be resolved and the resolve to decimal.

// example: converting a binary number into a hexadecimal number 10001000.

@ ideas: first binary to decimal conversion, and then converted from a decimal to hexadecimal.

  • var num1 = parseInt ( ‘10001000’, 2); // 136var num2 = num1.Tostring (16); // ’88’

  • 3, convert the value into a boolean type
Boolean (variable): Convert a value to its corresponding Boolean value.

(1) Conversion Method of Original Type Value

The following six values ​​of the conversion result is False, and the other values ​​are true for TRUE.


 +0   NAN 
” String)

(2) Conversion rule of the object

All Boolean values ​​of all objects are True, and even the Boolean objects corresponding to false are also TRUE.

Boolean (new boolean (false)) // true

  • Please note that empty object {} and empty An array [] will also be converted to TRUE.
  • Boolean ([]); // TrueBoolean (}); // true
  • Implicit Data Type Conversion
Conversion of implicit types is automatically performed when the system is calculated, but the method of call is an explicit type conversion method.

1, increment and decrement operators

A ++, A-, ++ A, –a

These four operators apply to any value, also Just apply not only to integers, but also for strings, boolean, floating point values, and objects, which are accompanied by implicit data type conversion.
The variable is first converted to Number data type by number (), and then increment, and decrement operation.   
2, (+) (-), ie the positive and negative number
not only applies to integers, but also for strings, boolean values, floats Point value and object. Convert the variable to the data type of Number ().

3, ISNAN (Variable)

The execution process is: i.e., first convert the variable through Number, and then perform ISNAN (). 4, (+) plus sign
  First look at the following code  

VAR STR = 1 + “1 “; var Num = 1 + 1; var Num1 = 1 + false; document.write (STR,”
“, NUM,”

“, Num1);

The execution result is: 11, 2, 1

So there are two effects. If there is no string during the operation, the variable is converted to the Number type through Number (). If there is a string, the plus sign is a string connection.

5, – * /% minus, multiplier, division, divide the data into the Number type, and then operates after the time


6, && ||! With or non-operation

Convert the value on both sides of the operator into a Boolean type, and then operate. Different, && || return is the original value of the comparison, but! The operation returned is a Boolean value. JavaScript的数据类型转换原则(干货)

Look at an example.

Console.log (5 && 3); // Judgment from left to right, if all are true, return the last one is a true value, as long as there is a judgment as a false, return to the value of the value console.log (0 || 2); // Judgment from left to right, return the first one as a true value, if all the judgment is completed, all the values ​​are false, returning the last value of the fake console.log (! 3

Returned: 3, 2, False.

= ==! = Comparison Operator
  When the number and string are more than a larger size, it is conceivable to convert a string into a Number type. When the string and string are relatively large, the ASCII code is compared. The last returned is Boolean  

// 1) Compare Alert between pure numbers (1
"ABC"); // False

Next, a special case is taken.

// Undefined does not have a type conversion console.log (undefined == undefined); // true console.log (undefined == 0); // False Console.log (undefined> 0); // false console.log (undefined

0); // false console.log (null

About == implicit type conversion, you can see blog: httpS: //

In the project engineering, if it is used to determine if the two values ​​are equal, the implicit type conversion occurs. So there is very large vulnerability. In order to solve this problem. Introduced === (absolutely equal) and! == (absolutely not equal).

Console.log (1 === “1”); // false console.log (1 === 1); // true
The above is the data type conversion summary of the JavaScript introduced to everyone, I hope to have Helped, if you have any questions, please leave a message, Xiaobian will reply to everyone in time. Thank you very much for your support of Tumi Cloud Website!

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment