当前位置:网站首页>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;
}
边栏推荐
- RT-Thread API参考手册
- Basic operations of databases and tables ----- view data tables
- Cannot change version of project facet Dynamic Web Module to 2.3.
- Principle and implementation of MySQL master-slave replication
- AMBA、AHB、APB、AXI的理解
- 互联网协议详解
- arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
- Apprentissage automatique - - régression linéaire (sklearn)
- Linux Yum install MySQL
- Inline detailed explanation [C language]
猜你喜欢
Gallery之图片浏览、组件学习
RT thread API reference manual
機器學習--線性回歸(sklearn)
Arduino uno R3 register writing method (1) -- pin level state change
ES6 grammar summary -- Part 2 (advanced part es6~es11)
电商数据分析--用户行为分析
MySQL数据库面试题
Togglebutton realizes the effect of switching lights
RT-Thread的main线程“卡死”的一种可能原因及解决方案
优先级反转与死锁
随机推荐
sklearn之feature_extraction.text.CountVectorizer / TfidVectorizer
MySQL realizes read-write separation
共用体(union)详解【C语言】
Cannot change version of project facet Dynamic Web Module to 2.3.
Important methods of array and string
imgcat使用心得
History object
JS变量类型以及常用类型转换
Common properties of location
FTP file upload file implementation, regularly scan folders to upload files in the specified format to the server, C language to realize FTP file upload details and code case implementation
Machine learning -- linear regression (sklearn)
I2C bus timing explanation
Pytoch temperature prediction
关键字 inline (内联函数)用法解析【C语言】
Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
Detailed explanation of 5g working principle (explanation & illustration)
Kconfig Kbuild
Vert. x: A simple TCP client and server demo
Basic operations of databases and tables ----- view data tables
Arduino JSON data information parsing