当前位置:网站首页>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
边栏推荐
- 败给“MySQL”的第60天,我重振旗鼓,四面拿下蚂蚁金服offer
- [SemiDrive source code analysis] [MailBox inter-core communication] 47 - Analysis of RPMSG_IPCC_RPC mode limit size of single transmission and limit bandwidth test
- Tactile intelligent sharing - SSD20X realizes upgrade display progress bar
- Will the 2023 PMP exam use the new version of the textbook?Reply is here!
- C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.2 Why does my code not work
- 力扣:343. 整数拆分
- 文献管理工具 | Zotero
- 编程大杂烩(三)
- [Evaluation model] Topsis method (pros and cons distance method)
- 力扣:746. 使用最小花费爬楼梯
猜你喜欢
随机推荐
震惊,99.9% 的同学没有真正理解字符串的不可变性
信息学奥赛一本通 1312:【例3.4】昆虫繁殖
C Expert Programming Chapter 5 Thinking about Linking 5.1 Libraries, Linking and Loading
The Road to Ad Monetization for Uni-app Mini Program Apps: Full Screen Video Ads
注意!软件供应链安全挑战持续升级
力扣:343. 整数拆分
少年成就黑客,需要这些技能
Use Patroni callback script to bind VIP pit
解决错误:npm WARN config global `--global`, `--local` are deprecated
npm报错Beginning October 4, 2021, all connections to the npm registry - including for package installa
docker安装mysql与宿主机相差8小时的问题。
动态规划总括
文献管理工具 | Zotero
Get the selected content of the radio box
Towards Real-Time Multi-Object Tracking(JDE)
读者让我总结一波 redis 面试题,现在肝出来了
[Skill] Using Sentinel to achieve priority processing of requests
Large chain best freight d audit with what software?What are the functions?
5个开源组件管理小技巧
How to keep the source code confidential in the development under the burning scenario









