当前位置:网站首页>JS cast and implicit type conversion and Unicode encoding

JS cast and implicit type conversion and Unicode encoding

2022-06-09 21:35:00 qq_ forty-six million three hundred and two thousand two hundre

Cast

One 、 Other data types Cast to String
Method 1: toString() Method

let a = 123
a =a.toString()
console.log(a,typeof(a),'111'); //123 string 111

 Insert picture description here

let b = null
b = b.toString()
console.log(b,typeof(b),'222'); //TypeError: Cannot read properties of null (reading 'toString')

let c = undefined
c = c.toString()
console.log(c,typeof(c),'222'); //TypeError: Cannot read properties of undefined (reading 'toString')

* Method 2:String() function *

let a = 123
 a = String(a)
 console.log(a,typeof(a),'111');  // 123 string 111

String() The function can also put null and undefined To string type ;

let b = null
b = String(b)
console.log(b,typeof(b),'222');  // null string 222

let c = undefined
c = String(c)
console.log(c,typeof(c),'333'); // undefined string 333

Two 、 Other data types Cast to Number
Method 1:Number() function
 Insert picture description here
 Insert picture description here

let name = ''
name = Number(name)
console.log(name,typeof(name)); //0 'number'

let sex = '    '
sex = Number(sex)
console.log(sex,typeof(sex)); //0 'number'
        
let a = "123"
a = Number(a)
console.log(a,typeof(a)); //123 'number'


let b = "123a"
b = Number(b)
console.log(b,typeof(b)) //NaN 'number'

let c = null
c = Number(c)
console.log(c,typeof(c)); //0 'number'

let d = undefined
d = Number(d)
console.log(d,typeof(d)); //NaN 'number'

Method 2:parseInt() 、parseFloat()
 Insert picture description here

let a = "123dfsl"
a = parseInt(a)
console.log(a,typeof(a)); //123 'number'

let b = "b123"
b = parseInt(b)
console.log(b,typeof(b)); //NaN 'number'

let c = "123.365ab"
c = parseInt(c)
console.log(c,typeof(c)); //123 'number'

let d = "123.365ab"
d = parseFloat(d)
console.log(d,typeof(d)); //123.365 'number'

 Insert picture description here

 let a = true
 a= parseInt(a)
 console.log(a,typeof(a)); //NaN 'number'

3、 ... and 、 Other data types Cast to Boolean
 Insert picture description here

Implicit type conversion

Logical operators !、&&、||
!( Not )
 Insert picture description here
Non operation will first put a To boolean type ,“hello” Of boolean The type is true, !a by false,!!a by true; therefore !!a Yes, it will a To its boolean value ;

&& ( And )
 Insert picture description here
 Insert picture description here
&&( And ) It's looking for false, If the first value is false, The second value will not be executed

||( or )
 Insert picture description here
 Insert picture description here

||( or ) It's looking for true, If the first value is true, The second value will not be executed .

 Insert picture description here

let a = 3 && 0
console.log(a); // 0

let a = NaN && 2
console.log(a); //NaN

let b = 'hellow' || ''
console.log(b); // hellow

Unicode code

 Insert picture description here

原网站

版权声明
本文为[qq_ forty-six million three hundred and two thousand two hundre]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206092059516641.html