js data type conversion

Data type conversion js

Display data type conversion

  1. typeof: operation to return six types of data: Number, String, Boolean, Objecet, undefined, function
        var a = 1; 
        //conloe.log(typeof(a)) 值为Number
        var a = [];
        //conloe.log(typeof(a)) 值为Object
        var a = {};
        //conloe.log(typeof(a)) 值为Object
        var a = null;
        //conloe.log(typeof(a)) 值为Object
        var a = true;
        //conloe.log(typeof(a)) 值为Boolean
        var d;
        //conloe.log(typeof(b)) 值为Undefined
        var a = function(){};
        //conloe.log(typeof(a)) 值为function
  1. Certain types of default installed for js, js string number will default implicit conversion
        console.log("1" + 1) //11
        console.log("1" + "1") //11
        console.log("1" * 1) //1
        console.log("1" -1) //0
        console.log("2" + 1) //21
        console.log("2"- 1) //1
  1. Number ( 'number') // converts digital stuff inside
       var demo = '123'
       demo =  Number(demo) //把demo装换为数字
       console.log(typeof(demo)) //number
       var demoB = true
       console.log(Number(demoB)) //1 boolean值转为数字类型以后1为真, 0为假
       console.log(Number("sfjafja")) //NaN 不能转为为数字的值转换为数字后, 值为NaN
       var demoN = null
       console.log(Number(demoN)) //值为0
       var demoun = undefined
       console.log(Number(demoun)) //NaN
  • the parseInt ():
    the parseInt (Val, the radix) inside the digital conversion integer, the first parameter can string, Boolean, or even objects, functions,
    the second parameter is a decimal, the value as the default of the hexadecimal number, binary is then converted to the base to decimal numbers
    again haircut: that is passed in the val as decimal radix, then he converted to decimal, the default value does not pass, then 10 ary
      var i = '123'
      console.log(parseInt(i), typeof(parseInt(i))) //123 number
      var i = "123.9"
      console.log(parseInt(i), typeof(parseInt(i))) //123 number
      var demoi = false
      console.log(parseInt(demoi), typeof(parseInt(demoi))) //NaN number
      var demor = 3
      console.log(parseInt(demor, 2)) // NaN , 默认把3当成2进制的数,由于2进制中不存在3, 所以输出为NaN
      var demorx = 'b'
      console.log(parseInt(demorx, 16)) //11 ,把b当成16进制数,然后把它转换为10进制数
      var demos = '100px'
      console.log(parseInt(demos)) //100 parseInt会从开头的数字开始看,看完不是数字为值
      var demols = "afaf100fafa" 
      console.log(parseInt(demols)) // NaN 
  • parseFloat (): parseFloat ( 'number') // usage and parseInt almost, but put all values ​​are converted to floating point
      console.log(parseFloat("100"))  //100 整数的时候parseFloat是不会添加.0 de
      console.log(parseFloat("100.1")) //100.1
      console.log(parseFloat('100.0.01')) //100 默认只会取一个小数点
      console.log(parseFloat('100.12raga')) //100.12
  • String ( 'val') // convert the string val
    var demostr = String(function (){
      console.log('I an String')
      })
    console.log(demostr) //function (){  console.log('I an String')}
  • toString ( 'mix') // into a string
      var demot = 1213
      var demots = toString(demot)
      var demounde = toString(undefined) 
      console.log(typeof (demounde)) //string
      console.log(typeof(demots)) //string
  • .toString (radix)
    wants to .tostring who later converted to a string can not be null and undefined toString
    can be converted into a string of the target number hexadecimal, decimal to binary conversion to the target base
    note distinguished parseInt (val , radix) as he is to val radix hex, then convert to decimal
      console.log(typeof(demot.toString()))
      var demo = 123
      console.log(demo.toString(8), typeof demo.toString(8)) //173 string

Implicit data type conversion

Implicit data type conversion under certain conditions, triggers the conversion mechanism within JavaScript, internal data type conversion call is displayed in
the main case implicit data type of trigger conditions: isNaN (), + // * // , ++, -!,>, > =, = <,> <,%,, &&, ||, ==, =!

  1. isNaN (val) // val is not a judgment of non-number, first call the Number function, then execute judgment
    console.log(isNaN('123')) //false
    console.log(isNaN('abc')) //true
    console.log(isNaN(null)) //false     Number(null) 是0 所以其值为false
    console.log(isNaN(undefined)) //true
  1. +/-, + / - * /%
    when it comes to these operators will find implicit conversion data types
    var abc = + "abc"
    console.log(typeof (abc), abc) //number NaN
    var abc = "abc" + 1
    console.log(typeof (abc), abc) //string abc1 当加号两侧有一个是字符串的时候就调用String()方法把它转换成String
    console.log(typeof ("1" * 1), 1 * "1") // numeber 1
    console.log(typeof (1 / "1"), 1 / 1) //number 1
    console.log(typeof (2 % "10"), 2 % "10") //numberi 2
  1. & gt & lt = & gt = & lt = ==! =
    digital comparator participation converted into digital type, no comparison will be converted to numeric code comparing ascall, Boolean types are converted to digital, digital principle of precedence
    console.log(1>= "2") //会转换成number型在进行比较
    console.log("1">"2") //会直接比ascall码值
    console.log(3 > true) //true
    console.log(1 == true) //true
    console.log(false == 1) //false
  1. Several special value
    null and undefined in comparison implicit conversion will not occur when
    ==! = Implicit conversion will occur ===! == implicit conversion will not take place
   console.log(Number(null)) //0
   console.log(null == 0)
   console.log(Number(null) == 0) //true
   console.log(1 == "1") //true 
   console.log(1 ==="1") //false
   console.log(1 === true)  //false
   console.log(1 != "1") //false
   console.log(1 !== "1") //true

Guess you like

Origin www.cnblogs.com/ghostdot/p/11372466.html