头图

foreword

Data type conversion is a scenario we often encounter in front-end development. Generally speaking, there are three cases of type conversion in JavaScript:

  • Convert to numbers (call Number(), parseInt(), parseFloat() methods)
  • Convert to string (call .toString() or String() method)
  • Convert to boolean (call Boolean() method)

It should be noted that: null, undefined does not have the .toString() method .

convert to number

Number(string) method

Number() can convert any value into a number. If the target object to be converted (usually a string) has a value that is not a number, it will return NaN .

Number('1')   // 1
Number(true)  // 1
Number('123s') // NaN
Number({})  //NaN

parseInt(string, radix)

Parses a string and returns a decimal integer in the specified radix, where radix is an integer between 2-36 representing the radix of the parsed string.

parseInt('2') //2
parseInt('2',10) // 2
parseInt('2',2)  // NaN
parseInt('a123')  // NaN  如果第一个字符不是数字或者符号就返回NaN
parseInt('123a')  // 123

parseFloat(string)

Parses an argument and returns a float.

parseFloat('123a')    //123
parseFloat('123a.01')    //123
parseFloat('123.01')    //123.01
parseFloat('123.01.1')    //123.01

implicit conversion

let str = '123'
-str    // -123
str+1 // '1231'
+str+1 // 124
let res = str - 1 //122

convert to string

toString()

Convert the target object to a string.

Note: null, undefined cannot be called.

Number(123).toString()    //'123'
[].toString()    //''
true.toString()    //'true'

String()

String() is more powerful, it can convert any incoming value into a string.

String(123)    //'123'
String(true)    //'true'
String([])    //''
String(null)    //'null'
String(undefined)    //'undefined'
String({})    //'[object Object]'

implicit conversion

When one of the two sides of + is a string and the other is another type, the other type will be converted to a string first, then the string will be concatenated, and the string will be returned:

let a = 1
a + ''    // '1'

convert to boolean

Boolean()

Boolean() method will convert the following values to false:

  • 0
  • ""
  • null
  • undefined
  • NaN

All remaining values are converted to true.

Boolean('') //false
Boolean(0) //false
Boolean(1) //true
Boolean(null) //false
Boolean(undefined) //false
Boolean(NaN) //false
Boolean({}) //true
Boolean([]) //true

Conditional statements

In conditional statements, we generally do not actively perform boolean conversions.

let a
if(a) {
  //...   //这里a为undefined,会转为false,所以该条件语句内部不会执行
}

implicit conversion

let str = '111'
console.log(!!str) // true

Summarize

The above is a summary of the knowledge related to JavaScript basic data type conversion, of which the most important thing is hermit conversion.

~

~End of this article, thanks for reading!

~

Learn interesting knowledge, meet interesting friends, and shape interesting souls!

Hello everyone, I'm Hermit King , the author Programming Samadhi 〗, my public is "161f378593f027 Programming Samadhi ", welcome to pay attention, I hope you can give me more advice!

You come, with expectations, I have the fragrance of ink to welcome you! You return, no matter what you gain or lose, you can only give it away with the aftertaste!

Both knowledge and skills are emphasized, internal and external skills are cultivated, both theory and practice must be grasped, and both hands must be hard!


编程三昧
54 声望10 粉丝

学习有趣的知识,交识有趣的朋友,造就有趣的灵魂!