当前位置:网站首页>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
边栏推荐
- 擎朗智能全国研发创新中心落地光谷:去年曾获2亿美元融资
- static在不同位置定义变量居然还有不同的含义?
- 使用Loadrunner进行性能测试
- npm报错Beginning October 4, 2021, all connections to the npm registry - including for package installa
- [One step in place] Jenkins installation, deployment, startup (complete tutorial)
- 深度学习环境配置
- string类简介
- [Cloud Native--Kubernetes] Pod Resource Management and Probe Detection
- 如何将 DevSecOps 引入企业?
- Gartner 权威预测未来4年网络安全的8大发展趋势
猜你喜欢
随机推荐
C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.2 我的代码为什么无法运行
Gartner 权威预测未来4年网络安全的8大发展趋势
力扣题解8/3
leetcode 12. 整数转罗马数字
败给“MySQL”的第60天,我重振旗鼓,四面拿下蚂蚁金服offer
Delphi-C端有趣的菜单操作界面设计
企业需要知道的5个 IAM 最佳实践
C Expert Programming Chapter 5 Thinking about Linking 5.2 Advantages of Dynamic Linking
What is the salary of a software testing student?
Use Patroni callback script to bind VIP pit
【一步到位】Jenkins的安装、部署、启动(完整教程)
DP4398:国产兼容替代CS4398立体声24位/192kHz音频解码芯片
路网编辑器技术预研
MySQL数据库面试题总结(2022最新版)
C专家编程 第5章 对链接的思考 5.2 动态链接的优点
C语言 -- 操作符详解
The Road to Ad Monetization for Uni-app Mini Program Apps: Full Screen Video Ads
Write golang simple C2 remote control based on gRPC
el-Select selector bottom fixed
2023年PMP考试会用新版教材吗?回复来了!









