当前位置:网站首页>JS basics - forced type conversion (error-prone, self-use)
JS basics - forced type conversion (error-prone, self-use)
2022-08-04 05:30:00 【ᥬ little moon】
1. Convert null and undefined to strings:
① Use the toString function: null and undefined have no tostring() method, and report an error directly
② Use String functions:
'null', 'undefined'
2. Convert other types to Number:
Use the Number function:
① number string --> number
② number + non-numeric string --> NAN
③ empty string --> 0
④ null --> 0
⑤ undefined --> NAN
⑥ true --> 1
⑦ false --> 0
Use the parseInt / parseFloat functions (specifically to convert strings):
① String --> Read the part of numbers from left to right, and stop when the part that is not a number is encountered.
② Non-string --> Convert it to a string and then operate:
- true -->NAN
Tips: parseInt has two parameters, the first parameter is the number to be converted, and the second parameter is the base to be converted.
3, base:
Starting with 0x: hexadecimal
- 0x16
Starting with 0: hexadecimal
- 070
Starting with 0b: binary
- 0b10
4. Convert other types to Boolean:
Use Boolean functions:
① Numbers: Except 0 and NAN are false, the rest are true
② String: Except the empty string is false, the rest are true
③ null: false
④ undefined: false
⑤ Object: true
边栏推荐
- MySQL数据库面试题总结(2022最新版)
- SLSA 框架与软件供应链安全防护
- 擎朗智能全国研发创新中心落地光谷:去年曾获2亿美元融资
- [Cloud Native--Kubernetes] Pod Resource Management and Probe Detection
- static在不同位置定义变量居然还有不同的含义?
- Shocked, 99.9% of the students didn't really understand the immutability of strings
- [Cocos 3.5.2]开启模型合批
- 8、自定义映射resultMap
- FPGA学习笔记——知识点总结
- Uni-app 小程序 App 的广告变现之路:全屏视频广告
猜你喜欢
随机推荐
leetcode 12. Integer to Roman numeral
符号表
嵌入式系统驱动初级【4】——字符设备驱动基础下_并发控制
OpenGL绘制一个圆锥
C Expert Programming Chapter 5 Thinking about Chaining 5.6 Take it easy --- see who's talking: take the Turning quiz
LCP 17. 速算机器人
Programming hodgepodge (4)
4.1 声明式事务之JdbcTemplate
C1认证之web基础知识及习题——我的学习笔记
使用Patroni回调脚本绑定VIP的坑
redis中常见的面试题
触觉智能分享-SSD20X实现升级显示进度条
Turn: Management is the love of possibility, and managers must have the courage to break into the unknown
有趣的 Kotlin 0x0E:DeepRecursiveFunction
OpenGL绘制圆
npm init [email protected] 构建项目报错SyntaxError: Unexpected token ‘.‘解决办法
el-Select selector bottom fixed
如何打造一篇优秀的简历
深度学习21天——卷积神经网络(CNN):实现mnist手写数字识别(第1天)
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









