Data type conversion of JS series (mandatory type conversion)

js basic data types: Number, String, Number, Null, Undefined, if you know these, just go ahead~

Convert data type to String

Conversion method 1: use the toString() method

Conversion method 2: use the String() function

      let a = 123;
      console.log(a.toString()) // '123'
      
      let b = true
      console.log(b.toString()) // 'true'

      let c = null;
      console.log(c.toString()) // 报错  

      let d = undefined
      console.log(d.toString()) // 报错
      
      let e = 123;
      console.log(String(d)) // '123'

      let f = true
      console.log(String(f)) // 'true'

      // null和undefined调用toString()或String()时不一样的
      let g = null;
      console.log(String(e)) // 'null'
      
      let h = undefined
      console.log(String(f)) // 'undefined'
Summarize:

​ 1. Calling toString() and String() will return string type data without changing the original data type

​ 2.toString() cannot convert null and undefined to strings. If you need to convert null and nudefined to strings, you can use String ()

Convert data type to Number type

​ Conversion method 1: use the Number() method

​ Conversion formula 2: use parseInt() method/parseFloat() method

      let a = '123'
      console.log(Number(a)) // 123
			
			// 空字符串
      let b = ''
      console.log(Number(b)) // 0

      let c = true
      console.log(Number(c)) // 1

      let d = false
      console.log(Number(b)) // 0
      
      let e = null;
      console.log(Number(e)) // 0

      let g = undefined
      console.log(Number(g)) // NaN
      
      // 如果纯数字和Number方法作用时一样的
      let h = '123'
      console.log(parseInt(h))

      // 如果不是纯数字则只取整数数字
      let i = '123.12px'
      console.log(parseInt(h)) // 123
      
      // parseFloat() 小数也会保留
      let j = '1321.144px'
      console.log(parseFloat(i)) // 1321.144

      //如果对非String使用parseInt()获知parseFloat()
      // 他会先将其转换成String然后再进行操作
   
      let k = true
      console.log(parseInt(k)) // NaN

      let l = null 
      console.log(parseInt(l)) // NaN

      let n = undefined
      console.log(parseInt(n)) // NaN
Summarize:

1. The string of pure numbers is converted directly through the Number() function

2. If it is not a pure number and uses Number conversion, the type becomes NaN, but the data type is still number type

​ 3. If there are spaces in the string, if there are no characters, it will be converted to 0

​ 4. Boolean value to digital true is 1, false is 0

​ 5. Null to digital null is 0

6. undefined is NaN

Two methods for special treatment of strings

parseInt()

1. The string effect on pure numbers is consistent with Number()

2. Non-pure numbers, if they do not start with English, only integer numbers are taken

3. Non-pure numbers, starting with English is NaN

4.parseInt() can be used to take an integer

​ pressFloat()

1. The string effect on pure numbers is consistent with Number()

​ 2. Non-pure numbers, all numbers will be taken if they do not start with English

3. Non-pure numbers, starting with English is NaN

Convert data type to Boolean

Conversion method: use Boolean()

      let a = 123
      console.log(Boolean(a)) // true
      
      let b = -123
      console.log(Boolean(b)) // true

      let c = 0
      console.log(Boolean(c)) // false

      let d = NaN 
      console.log(Boolean(d)) // false

      let e = 'test'
      console.log(Boolean(e)) // true
      
      let f = ''
      console.log(Boolean(f)) // false

      let g = null
      console.log(Boolean(g)) // false

      let h = undefined
      console.log(Boolean(h)) // fasle
			
			// 对象也为true
      let i = {
        name: '法外狂徒',
        age: 22
      }

      console.log(Boolean(i)) // true
Summarize:

1. Number type to Boolean type: except 0 and NaN, the rest are true

2. String type to Boolean type: except for the empty string, the rest are true

​ 3. Both null and undefined will be converted to false

4. The object will also be converted to true

Conclusion:

​ There are only a few types of data in js, but it is still a bit of a pain in the head to turn around the type. Familiarity with the conversion of data types will allow us to avoid some minor problems when writing code. Everyone usually writes bugs Pay more attention to it, it is still no problem, friends, come on, I wish you all the best in your work~

The above is just my personal opinion, please correct me if there are any mistakes, thank you~

Guess you like

Origin blog.csdn.net/Liushiliu104/article/details/120299141