当前位置:网站首页>JS variable types and common type conversions
JS variable types and common type conversions
2022-07-06 12:09:00 【Aboci Bang】
JS Variable type
The original type :Number、String、Boolern、undefined、NULL– Store in the stack
Reference type :Object -- Store in the pile
1. Numerical type –Number: Numbers
(1) integer : Positive and negative integers and zero
(2) floating-point : decimal (1.11e+1 = 11.1)
2. String type –String
single quote (‘abc’)、 Double quotes (“abc”)、 Template string (``) Wrapped character set .
3. Boolean type –Boolern
true and false
4. Undefined type –undefined
5. Empty type –NULL
Its type is actually an object
6. object type –Object
Objects store properties and methods
var obj = {
a:a,
b:func1
}
classification :
Host object The host is the environment
There are two kinds of environments that the front end contacts :
1. Browser environment -- Provided window object ( Global object ) And its children
2. Server environment – nodejs Environment provided – global object ( Global object ) And its children
Both environments can run js Code .
The same thing is that they all use ChromeV8 engine – Because it is the highest performance engine at present
The difference is The two environments provide API Different
Built-in objects
JavaScript Provides some built-in objects and functions :
String、Math、Date、Array…
Custom object
Class instantiation object defined by ourselves
7.Symbol type
Please check the details
https://es6.ruanyifeng.com/#docs/symbol
7.BigInt type
var bg = 121212n; // Add... After the number ‘n’
Be careful :
1.BigInt Type cannot be used Math Object method .
2. Unable to join Number Example hybrid operation .
Type conversion
It is divided into : Implicit conversion and cast
Other types are converted to numbers :
Implicit conversion – Automatic conversion during program operation 

The plus sign is compared with other operators first except Arithmetic operation also String concatenation The role of
As shown in the figure above : Spliced strings have priority over arithmetic operations ( Will first string splicing If there is no string splicing, the arithmetic operation will be performed )
true Implicitly convert to 1
false and null Implicitly convert to 0
undefined Into the NaN =>Not a Number
reduce 、 ride 、 except 、 modulus Can convert a string into a number Convert a number to a string (1+’’) that will do
~~ Operators can be used properly to easily convert strings and numbers into each other in work
NaN operation 
NaN The results of mathematical operations are NaN Even if it's NaN It is also NaN
The function used for implicit conversion is Number
Coercive transformation – Manual switching
Forced conversion to numbers
Number(‘1’) //1
Number(‘1a’) //NaN
Number(true) //1
Number(false) //0
Number(undefined) //NaN
Number(null) //0
Forced to integer
You can only turn numbers Encounter letters or decimal points or other characters Stop intercepting The result of conversion failure is NaN
parseInt Back to decimal perhaps NaN, It has two parameters parseInt( Values to convert , Converted base ) The first white space character of the string is ignored
Force conversion to floating point 
Encounter letters, etc Stop intercepting The result of conversion failure is NaN The first white space character of the string is ignored
Force to string 
notes : If directly in numbers 、true And so on API You can use Numbers + Space +.API
Strong to Boolean 
Turn Boolean to false Of situation :’’ NaN 0 null undefined
Implicitly convert to Boolean
!!num // Double exclamation + Variable name
~~// I often write this in the program First, verify the errors we can expect at the beginning of the method If something goes wrong be return;
if(!num){
// Error prompt code
return;
}
边栏推荐
猜你喜欢

Gallery之图片浏览、组件学习

ES6语法总结--下篇(进阶篇 ES6~ES11)

Principle and implementation of MySQL master-slave replication

ESP学习问题记录

Basic use of pytest

Missing value filling in data analysis (focus on multiple interpolation method, miseforest)

【ESP32学习-2】esp32地址映射

Kconfig Kbuild

Esp8266 uses Arduino to connect Alibaba cloud Internet of things

ToggleButton实现一个开关灯的效果
随机推荐
Pytorch-温度预测
Reno7 60W超级闪充充电架构
5G工作原理详解(解释&图解)
关键字 inline (内联函数)用法解析【C语言】
.elf .map .list .hex文件
Correspondence between STM32 model and contex M
Keyword inline (inline function) usage analysis [C language]
Arduino JSON data information parsing
C语言,log打印文件名、函数名、行号、日期时间
Contiki source code + principle + function + programming + transplantation + drive + network (turn)
Rough analysis of map file
Machine learning -- linear regression (sklearn)
ARM PC=PC+8 最便于理解的阐述
STM32 如何定位导致发生 hard fault 的代码段
树莓派 轻触开关 按键使用
js 变量作用域和函数的学习笔记
Fashion-Gen: The Generative Fashion Dataset and Challenge 论文解读&数据集介绍
【ESP32学习-2】esp32地址映射
嵌入式启动流程
vim命令行笔记