当前位置:网站首页>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;
}
边栏推荐
- Variable star user module
- RuntimeError: cuDNN error: CUDNN_ STATUS_ NOT_ INITIALIZED
- STM32 如何定位导致发生 hard fault 的代码段
- 小天才电话手表 Z3工作原理
- Variable parameter principle of C language function: VA_ start、va_ Arg and VA_ end
- 共用体(union)详解【C语言】
- Arduino gets the length of the array
- Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
- Pytorch-温度预测
- ESP learning problem record
猜你喜欢
Vert. x: A simple login access demo (simple use of router)
Machine learning -- decision tree (sklearn)
【ESP32学习-2】esp32地址映射
Common properties of location
Fashion Gen: the general fashion dataset and challenge paper interpretation & dataset introduction
Vscode basic configuration
MySQL realizes read-write separation
open-mmlab labelImg mmdetection
ESP8266使用arduino连接阿里云物联网
荣耀Magic 3Pro 充电架构分析
随机推荐
Reno7 60W super flash charging architecture
Basic use of pytest
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
GCC compilation options
Detailed explanation of 5g working principle (explanation & illustration)
ES6 grammar summary -- Part I (basic)
Mysql database interview questions
优先级反转与死锁
ARM PC=PC+8 最便于理解的阐述
Priority inversion and deadlock
E-commerce data analysis -- salary prediction (linear regression)
MySQL START SLAVE Syntax
Characteristics, task status and startup of UCOS III
Esp8266 connects to onenet cloud platform (mqtt) through Arduino IDE
ESP学习问题记录
C语言,log打印文件名、函数名、行号、日期时间
Fashion Gen: the general fashion dataset and challenge paper interpretation & dataset introduction
arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
STM32 如何定位导致发生 hard fault 的代码段
Amba, ahb, APB, Axi Understanding