当前位置:网站首页>JS基础--强制类型转换(易错点,自用)
JS基础--强制类型转换(易错点,自用)
2022-08-04 05:22:00 【ᥬ 小月亮】
1、将null和undefined转换为字符串:
① 使用toString函数:null和undefined没有tostring()方法,直接报错
② 使用String函数:
'null'、'undefined'
2、将其他类型转换为Number:
使用Number函数:
① 数字字符串 --> 数字
② 数字+非数字字符串 --> NAN
③ 空串 --> 0
④ null --> 0
⑤ undefined --> NAN
⑥ true --> 1
⑦ false --> 0
使用 parseInt / parseFloat
函数(专门用来转换字符串):
① 字符串 --> 从左向右读取数字的部分,遇到不是数字的部分就会停止。
② 非字符串 --> 将其转换为字符串再进行操作:
- true --> NAN
Tips: parseInt有两个参数,第一个参数是需要转换的数字,第二个参数表示想要转换的进制。
3、进制:
0x开头:16进制
- 0x16
0开头:8进制
- 070
0b开头:2进制
- 0b10
4、将其他类型转换为Boolean:
使用Boolean函数:
① 数字:除了0和NAN是false,其余都是true
② 字符串:除了空串是false,其余都是true
③ null:false
④ undefined:false
⑤ 对象:true
边栏推荐
- Write golang simple C2 remote control based on gRPC
- 离线采集怎么看sql执行计划
- 【一步到位】Jenkins的安装、部署、启动(完整教程)
- 应届生软件测试薪资大概多少?
- Get the selected content of the radio box
- C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.2 我的代码为什么无法运行
- Programming hodgepodge (4)
- How to view sql execution plan offline collection
- C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.4 使声明与定义相匹配
- What is the salary of a software testing student?
猜你喜欢
字节最爱问的智力题,你会几道?
docker安装mysql与宿主机相差8小时的问题。
Turn: Management is the love of possibility, and managers must have the courage to break into the unknown
Typora 使用保姆级教程 | 看这一篇就够了 | 历史版本已被禁用
System design. How to design a spike system (full version transfer)
The idea setting recognizes the .sql file type and other file types
[21 Days Learning Challenge] Image rotation problem (two-dimensional array)
3000字,一文带你搞懂机器学习!
一个对象引用的思考
编程大杂烩(四)
随机推荐
C Expert Programming Chapter 5 Thinking about Linking 5.2 Advantages of Dynamic Linking
Programming hodgepodge (4)
Shocked, 99.9% of the students didn't really understand the immutability of strings
信息学奥赛一本通 1312:【例3.4】昆虫繁殖
【一步到位】Jenkins的安装、部署、启动(完整教程)
C Expert Programming Chapter 4 The Shocking Fact: Arrays and Pointers Are Not the Same 4.3 What is a Declaration and What is a Definition
What is the salary of a software testing student?
编程大杂烩(四)
数的划分之动态规划
The difference between px, em, and rem
备份工具pg_dump的使用《postgres》
关于yolo7和gpu
深度学习21天——准备(环境配置)
px、em、rem的区别
Cache pool of unity framework
【流程图】
如何低成本修bug?测试左移给你答案
BFC、IFC、GFC、FFC概念理解、布局规则、形成方法、用处浅析
Jenkins export and import Job Pipeline
应届生软件测试薪资大概多少?