当前位置:网站首页>Conversion of data types
Conversion of data types
2022-07-18 18:48:00 【InfoQ】
Introduction to data type conversion
var x = y ? 1 : 'a';
xyytruexyfalsexx'4' - '3' // 1
1Coercive transformation ( Explicit conversion )
Number()String()Boolean()Number( )
Number// The number : After conversion, it is still the original value
Number(324) // 324
// character string : If it can be solved numerically , Then convert to the corresponding value
Number('324') // 324
// character string : If it can't be solved numerically , return NaN
Number('324abc') // NaN
// Empty string to 0
Number('') // 0
// Boolean value :true Turn into 1,false Turn into 0
Number(true) // 1
Number(false) // 0
// undefined: Turn into NaN
Number(undefined) // NaN
// null: Turn into 0
Number(null) // 0
NumberparseIntNaNparseInt('42 cats') // 42
Number('42 cats') // NaN
parseIntNumberparseIntNumberparseInt('\t\v\r12.34\n') // 12
Number('\t\v\r12.34\n') // 12.34
NumberNaNNumber({a: 1}) // NaN
Number([1, 2, 3]) // NaN
Number([5]) // 5
NumbervalueOfNumbervalueOftoStringtoStringNumbertoStringvar obj = {x: 1};
Number(obj) // NaN
// Equate to
if (typeof obj.valueOf() === 'object') {
Number(obj.toString());
} else {
Number(obj.valueOf());
}
Numberobjobj.valueOfobj.toString[object Object]NumberNaNvalueOftoStringtoString[object Object]Number({}) // NaN
toStringvar obj = {
valueOf: function () {
return {};
},
toString: function () {
return {};
}
};
Number(obj)
// TypeError: Cannot convert object to primitive value
valueOftoStringvalueOftoStringNumber({
valueOf: function () {
return 2;
}
})
// 2
Number({
toString: function () {
return 3;
}
})
// 3
Number({
valueOf: function () {
return 2;
},
toString: function () {
return 3;
}
})
// 2
NumbervalueOftoStringvalueOftoStringString( )
String- The number: To the corresponding string .
- character string: After conversion, it is still the original value .
- Boolean value:
trueTo string"true",falseTo string"false".
- undefined: To string
"undefined".
- null: To string
"null".
String(123) // "123"
String('abc') // "abc"
String(true) // "true"
String(undefined) // "undefined"
String(null) // "null"
StringString({a: 1}) // "[object Object]"
String([1, 2, 3]) // "1,2,3"
StringNumbervalueOftoString- Call the object's own
toStringMethod . If the value of the original type is returned , Use... For this valueStringfunction , Don't do the following steps .
- If
toStringMethod returns an object , Call the original object againvalueOfMethod . IfvalueOfMethod returns the value of the original type , Use... For this valueStringfunction , Don't do the following steps .
- If
valueOfMethod returns an object , Just report a mistake .
String({a: 1})
// "[object Object]"
// Equate to
String({a: 1}.toString())
// "[object Object]"
toString[object Object]valueOftoStringvalueOfvar obj = {
valueOf: function () {
return {};
},
toString: function () {
return {};
}
};
String(obj)
// TypeError: Cannot convert object to primitive value
toStringString({
toString: function () {
return 3;
}
})
// "3"
String({
valueOf: function () {
return 2;
}
})
// "[object Object]"
String({
valueOf: function () {
return 2;
},
toString: function () {
return 3;
}
})
// "3"
StringtoStringtoString[object Object]toStringvalueOfBoolean( )
Boolean()falsetrueundefined
null
0( contain-0and+0)
NaN
''( An empty string )
Boolean(undefined) // false
Boolean(null) // false
Boolean(0) // false
Boolean(NaN) // false
Boolean('') // false
truefalseBoolean(true) // true
Boolean(false) // false
truefalsenew Boolean(false)trueBoolean({}) // true
Boolean([]) // true
Boolean(new Boolean(false)) // true
trueobj1 && obj2trueAutomatic conversion ( Implicit conversion )
123 + 'abc' // "123abc"
if ('abc') {
console.log('hello')
} // "hello"
+-+ {foo: 'bar'} // NaN
- [1, 2, 3] // NaN
String()Boolean()Number()String()Automatically convert to Boolean
ifBoolean()trueundefined
null
+0or-0
NaN
''( An empty string )
falsetrueif ( !undefined
&& !null
&& !0
&& !NaN
&& !''
) {
console.log('true');
} // true
Boolean()// Writing a
expression ? true : false
// Write two
!! expression
Automatically convert to string
'5' + 1 // '51'
'5' + true // "5true"
'5' + false // "5false"
'5' + {} // "5[object Object]"
'5' + [] // "5"
'5' + function (){} // "5function (){}"
'5' + undefined // "5undefined"
'5' + null // "5null"
var obj = {
width: '100'
};
obj.width + 20 // "10020"
12010020Automatically convert to values
Number()+'5' - '2' // 3
'5' * '2' // 10
true - 1 // 0
false - 1 // -1
'1' - 1 // 0
'5' * [] // 0
false / '5' // 0
'abc' - 1 // NaN
null + 1 // 1
undefined + 1 // NaN
null0undefinedNaN+'abc' // NaN
-'abc' // NaN
+true // 1
-false // 0
- JavaScript How to convert data types in ?
+- Convert to numeric type :
Number(),parseInt(),parseFloat()
- Convert to boolean type :
Boolean()
- Convert to string type :
toString(),String()
- Convert string : Directly spliced with an empty string , for example :
a = "" + data
- Convert Boolean :!! data type , for example :
!!"Hello"
- Convert values : data *1 or /1, for example :
"Hello * 1"
边栏推荐
- 让我们自定义反射系统
- STM32 - timer interrupt experiment
- [graduation project] network public opinion hotspot analysis system based on Emotional Analysis
- Preparing transaction:done Verifying transaction:failed RemoveError:‘requests‘ is a dependency of **
- 【Ucos-III源码分析】——事件标志组
- Anaconda 的认识以及和它相关的一些编辑器的简单介绍
- Decoding of relative motion of two moving points
- P1789 [MC survival] torch [introduction]
- Halcon and C # detect surface defects - ROI interaction (II) (functions such as synchronous scaling and clipping with pictures)
- 自己第一次正畸情况(持续更新中)
猜你喜欢

一文详解图像中的无监督学习

Redis 与 Mysql 双写一致性方案解析

PNAs | Nannong zhangruifu group revealed a new signal recognition mechanism of microbial fertilizer functional mycorrhizal chemotaxis

Decoding of relative motion of two moving points

Talk about promise

ctf-pikachu-sql

UPUPWANK柚皮安装SSL证书指南

Summary of the method of distinguishing version number for fastjson deserialization vulnerability

impala高级设置之BROADCAST_BYTES_LIMIT

文件的使用详解
随机推荐
【C语言刷LeetCode】539. 最小时间差(M)
STL string
Flink CEP - 复杂事件处理(Complex Event Processing)
双动点相对运动破解
CAN光纤转换器解决泰和安TX3016A消防主机长距离联网问题
ssd训练自己的数据集
服装ERP怎么选?选型时一定要问这些问题
STM32按键外部中断控制LED流水灯-HAL库
文件的使用详解
scrapy-redis分布式爬虫部署
Read write lock principle in golang
MySQL笔记: B站宋红康最新教程(持续更新中)
Stream—优雅的处理集合元素
【Ucos-III源码分析】——等待多个内核对象
Arthas的常见用法
Anaconda 的认识以及和它相关的一些编辑器的简单介绍
要做的事情
STM32-中断优先级管理NVIC详解
软件测试面试题:软件产品质量特性是什么?
uCOS-III学习笔记——软件定时器