当前位置:网站首页>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
边栏推荐
- System design. Seckill system
- C Expert Programming Chapter 5 Thinking about Linking 5.1 Libraries, Linking and Loading
- C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.4 Matching declarations to definitions
- 信息学奥赛一本通 1312:【例3.4】昆虫繁殖
- DataTable使用Linq进行分组汇总,将Linq结果集转化为DataTable
- C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.1 Arrays are not pointers
- [SemiDrive source code analysis] [MailBox inter-core communication] 47 - Analysis of RPMSG_IPCC_RPC mode limit size of single transmission and limit bandwidth test
- 少年成就黑客,需要这些技能
- 2022软件测试面试题 最新字节跳动50道真题面试题 刷完已拿下15k 附讲解+答疑
- 字节最爱问的智力题,你会几道?
猜你喜欢

编程大杂烩(三)

腾讯136道高级岗面试题:多线程+算法+Redis+JVM

附加:对于“与数据表对应的实体类“,【面对MongoDB时,使用的@Id等注解】和【以前面对MySQL时,使用的@Id等注解】,是不同的;

【一步到位】Jenkins的安装、部署、启动(完整教程)

npm安装依赖报错npm ERR! code ENOTFOUNDnpm ERR! syscall getaddrinfonpm ERR! errno ENOTFOUND

Get the selected content of the radio box

【C语言进阶】程序环境和预处理

Shocked, 99.9% of the students didn't really understand the immutability of strings

深度学习21天——准备(环境配置)

System design. Seckill system
随机推荐
编程大杂烩(三)
Interesting Kotlin 0x0E: DeepRecursiveFunction
npm报错Beginning October 4, 2021, all connections to the npm registry - including for package installa
触觉智能分享-SSD20X实现升级显示进度条
【21 Days Learning Challenge】Direct Insertion Sort
The idea setting recognizes the .sql file type and other file types
某母婴小程序加密参数解密
Structure function exercise
[Cloud Native--Kubernetes] Pod Resource Management and Probe Detection
How to keep the source code confidential in the development under the burning scenario
Chapter 5 C programming expert thinking 5.4 alert Interpositioning of links
Towards Real-Time Multi-Object Tracking (JDE)
leetcode 12. 整数转罗马数字
C Expert Programming Chapter 5 Thinking about Linking 5.1 Libraries, Linking and Loading
代码重构:面向单元测试
What are the steps for how to develop a mall system APP?
idea设置识别.sql文件类型以及其他文件类型
Will the 2023 PMP exam use the new version of the textbook?Reply is here!
See how DevExpress enriches chart styles and how it empowers fund companies to innovate their business
如何将 DevSecOps 引入企业?