当前位置:网站首页>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
边栏推荐
- Typora 使用保姆级教程 | 看这一篇就够了 | 历史版本已被禁用
- 力扣:343. 整数拆分
- 入坑软件测试的经验与建议
- Can‘t connect to MySQL server on ‘localhost3306‘ (10061) 简洁明了的解决方法
- C Expert Programming Chapter 5 Thinking about Linking 5.1 Libraries, Linking and Loading
- [SemiDrive source code analysis] [MailBox inter-core communication] 47 - Analysis of RPMSG_IPCC_RPC mode limit size of single transmission and limit bandwidth test
- 3面头条,花7天整理了面试题和学习笔记,已正式入职半个月
- redis中常见的面试题
- Uni-app 小程序 App 的广告变现之路:全屏视频广告
- Landing, the IFC, GFC, FFC concept, layout rules, forming method, use is analysed
猜你喜欢
随机推荐
8大软件供应链攻击事件概述
9、动态SQL
基于gRPC编写golang简单C2远控
MySQL日期函数
DataTable uses Linq for grouping and summarization, and converts the Linq result set into DataTable
TSF微服务治理实战系列(一)——治理蓝图
力扣:509. 斐波那契数
[Cloud Native--Kubernetes] Pod Resource Management and Probe Detection
C1认证之web基础知识及习题——我的学习笔记
使用Loadrunner进行性能测试
The 2022 PMP exam has been delayed, should we be happy or worried?
day13--postman interface test
Delphi-C端有趣的菜单操作界面设计
C Expert Programming Chapter 4 The Shocking Fact: Arrays and Pointers Are Not the Same 4.5 Other Differences Between Arrays and Pointers
触觉智能分享-SSD20X实现升级显示进度条
px、em、rem的区别
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
OpenGL绘制一个圆锥
力扣:70. 爬楼梯
BFC、IFC、GFC、FFC概念理解、布局规则、形成方法、用处浅析