Home > Article > Web Front-end > What are the two types of data type conversion in JavaScript?
There are two types of data type conversion in JavaScript: 1. Explicit type conversion (also called forced type conversion), which mainly converts data by using JavaScript’s built-in functions; 2. Implicit type conversion, which refers to JavaScript based on The computing environment automatically converts the value's type.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
JavaScript is a weakly typed dynamically typed language. Variables have no type restrictions and can be assigned any value at any time.
var x = y ? 1 : 'a';
In the above code, whether the variable x is a numerical value or a string depends on the value of another variable y. When y is true, x is a numeric value; when y is false, x is a string. This means that the type of x cannot be known at compile time and must wait until runtime.
Although the data type of the variable is uncertain, various operators have requirements for the data type. If the operator finds that the type of the operator does not match the expected type, it will automatically convert the type. For example, the subtraction operator expects that the left and right operators should be numeric values, and if not, it will automatically convert them to numeric values.
'4' - '3' // 1
In the above code, although two strings are subtracted, the result value 1 will still be obtained. The reason is that JavaScript automatically converts the operator into a numerical value.
Data type conversion in javascript
Data type conversion in js is generally divided into two types, namely forced type conversion and implicit type Conversion (using js weak variable type conversion).
Explicit type conversion is mainly done by using JavaScript’s built-in functions;
Implicit type conversion means JavaScript automatically converts according to the computing environment The type of value.
In js, if you want to convert an object into a primitive value, you must call the toPrimitive() internal function, so how does it work?
f35d6e602fd7d0f0edfa6f7d103c1b57 toPrimitive(input,preferredType)
input is the input value, preferredType is the type expected to be converted, it can be String or Number, or not passed.
1) If the converted type is number, the following steps will be performed:
1. 如果input是原始值,直接返回这个值; 2. 否则,如果input是对象,调用input.valueOf(),如果结果是原始值,返回结果; 3. 否则,调用input.toString()。如果结果是原始值,返回结果; 4. 否则,抛出错误。
2) If the converted type is String, 2 and 3 will be executed interchangeably, that is, toString will be executed first. ()method.
3) You can omit preferredType. At this time, the date will be considered as a string, and other values will be treated as Number.
① If the input is the built-in Date type, the preferredType is regarded as String
② Otherwise, it is regarded as Number, first call valueOf, and then call toString
2cc198a1d5eb0d3eb508d858c9f5cbdbToBoolean( argument)
Type | Return result |
Underfined | false |
Null | false |
Boolean | argument |
Number | Only when argument is 0, -0 or NaN, return false; otherwise return true |
String | Only when argument is an empty string ( When the length is 0), return false; otherwise return true |
Symbol | true |
Object | true |
Note: Except underfined,null,false,NaN,'',0,-0, all others return true
5bdf4c78156c7953567bb5a0aef2fc53ToNumber(argument)
Type | Return result |
Underfined | NaN |
Null | 0 |
Boolean | argument is true, return 1; is false, return 0 |
Number | argument |
String | replace the content in the string Convert to a number, such as '23'=>23; If the conversion fails, return NaN, such as '23a'=>NaN |
Symbol | throw TypeError exception |
**First primValue= toPrimitive(argument,number), then use ToNumber(primValue)** | # for primValue |
Underfined | |
Null | |
Boolean | |
Number | |
String | |
Symbol | |
Object | |
## 1.隐式类型转换: 1.1-隐式转换介绍 · 在js中,当运算符在运算时,如果两边数据不统一,CPU就无法计算,这时我们编译器会自动将运算符两边的数据做一个数据类型转换,转成一样的数据类型再计算 这种无需程序员手动转换,而由编译器自动转换的方式就称为隐式转换 · 例如1 > "0"这行代码在js中并不会报错,编译器在运算符时会先把右边的"0"转成数字0`然后在比较大小 (1). 转成string类型: +(字符串连接符) 2.强制类型(显式类型)转换: 通过手动进行类型转换,Javascript提供了以下转型函数:
2.1 Boolean(value)、Number(value) 、String(value) new Number(value) 、new String(value)、 new Boolean(value)传入各自对应的原始类型的值,可以实现“装箱”-----即将原始类型封装成一个对象。其实这三个函数不仅可以当作构造函数,还可以当作普通函数来使用,将任何类型的参数转化成原始类型的值。 其实这三个函数在类型转换的时候,调用的就是js内部的ToBoolean(argument)、ToNumber(argument)、ToString(argument)
let objj={ valueOf:function(){return '2px'}, toString:function(){return []} } parseInt(objj) //2 parseInt('001') //1 parseInt('22.5') //22 parseInt('123sws') //123 parseInt('sws123') //NaN //特殊的 parseInt(function(){},16) //15 parseInt(1/0,19) //18 //浏览器代码解析器:parseInt里面有两个参数,第二个参数是十九进制(0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i),额,1/0,好吧先运算 结果等于Infinity, //I好的十九进制有认识,n十九进制不存在不认识,不管后面有没有了,立即返回i(i对应的十进制中的18),所以返回18 parseInt(1/0,16) //NaN //同上,16进制灭有对应i,返回NaN parseInt(0.0000008) //8 //String(0.0000008),结果为8e-7 parseInt(0.000008) //0 parseInt(false,16) //250 //16进制,'f'认识, 'a'认识, 'l'哦,不认识,立即返回fa (十六进制的fa转换成十进制等于250) parseInt('0x10')) //16 //只有一个参数,好的,采用默认的十进制, '0x',额,这个我认识,是十六进制的写法, 十六进制的10转换成十进制等于16 parseInt('10',2) //2 //返回二进制的10 转换成十进制等于2 2.3 parseFloat(string) 将字符串转换为浮点数类型的数值.规则:
2.4 toString(radix) 除undefined和null之外的所有类型的值都具有toString()方法,其作用是返回对象的字符串表示 【相关推荐:javascript学习教程】 |
The above is the detailed content of What are the two types of data type conversion in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!