JavaScript learning record 6: data type conversion

1, What is data type conversion

The data obtained by using the form and prompt is of string type by default. At this time, you can't directly and simply add, but need to convert
The data type of the quantity. Generally speaking, it is to convert a variable of one data type into another data type.
We usually implement the conversion in three ways:

  • Convert to string type
  • Convert to numeric
  • Convert to Boolean

2, Convert other types to string types

modeexplainexample
toString()Convert to stringvar num=1; alert(num.toString();
String() castConvert to stringvar num=1; alert(String(num));
Plus concatenated stringAnd string splicing results are stringsvar num=1; alert (Num + "I am a string")
  • toString() and String() are used differently.
  • Among the three conversion methods, we prefer the third plus sign splicing string conversion method, which is also called implicit conversion.

3, Other types to digital types

modeexplainexample
parselnt (string) functionConvert string type to floating-point numeric typeparselnt(78')
parseFloat (string) functionConvert string type to floating-point numeric typeparseFloat(78.21')
Number() cast functionConvert string type to numeric typeNumber('12)
js implicit conversion (- * /)Implicit conversion to numerical type by arithmetic operation'12'-0

be careful

  • The case of parseInt and parseFloat words, which are the key points
  • Implicit conversion is that JS automatically converts data types when we perform arithmetic operations

1.parseInt() conversion

   var a = 1
    var b = '112'
    var c = 'Finger sucking plain chicken, not chicken legs'
    var d = ''
    var e = '112 Finger sucking plain chicken, not chicken legs'
    var f = 'Finger sucking plain chicken, not chicken legs 112'
    var g = 'true'
    var h=  'false'
    var i = true
    var j = false
    var k = undefined
    var l =null
    var m = 1.123
    console.log(parseInt(a)) //1
    console.log(parseInt(b)) //112
    console.log(parseInt(c)) //NaN
    console.log(parseInt(d)) //NaN
    console.log(parseInt(e)) //112
    console.log(parseInt(f)) //NaN
    console.log(parseInt(g)) //NaN
    console.log(parseInt(h)) //NaN
    console.log(parseInt(i)) //NaN
    console.log(parseInt(k)) //NaN
    console.log(parseInt(l)) //NaN
    console.log(parseInt(m)) //1

parseInt() converts a data type to a number type. It starts from left to right and knows that characters that are not numbers are encountered. Numbers are still numbers, but decimal types only go to integers. They can't recognize decimal points. Characters that can't be converted to numbers become NaN, not a number.
To sum up: only remove the part where the string prefix is a number.
2.parseFloat() conversion

   var a = 1
    var b = '112'
    var c = 'Finger sucking plain chicken, not chicken legs'
    var d = ''
    var e = '112 Finger sucking plain chicken, not chicken legs'
    var f = 'Finger sucking plain chicken, not chicken legs 112'
    var g = 'true'
    var h=  'false'
    var i = true
    var j = false
    var k = undefined
    var l = null
    var m = 1.123
    console.log(parseFloat(a))  //1
    console.log(parseFloat(b))  //112
    console.log(parseFloat(c))  //NaN
    console.log(parseFloat(d))  //NaN
    console.log(parseFloat(e))  //112
    console.log(parseFloat(f))  //NaN
    console.log(parseFloat(g))  //NaN
    console.log(parseFloat(h))  //NaN
    console.log(parseFloat(i))  //NaN
    console.log(parseFloat(k))  //NaN
    console.log(parseFloat(l))  //NaN
    console.log(parseFloat(m))  //1.123

To sum up, parseFloat is similar to parseInt, but parseFloat can recognize the decimal point.
3.Number conversion

 var a = 1
    var c = 'Finger sucking plain chicken, not chicken legs'
    var d = ''
    var g = 'true'
    var h=  'false'
    var i = true
    var j = false
    var k = undefined
    var l =null
    var m = 1.123
    var n = {}
    var o ={name:"qq"}
    var p =[]
    var q = [1,3,4]
    var r = 0
    var s ='0'
    console.log(Number(a))//Number or number
    console.log(Number(c))//The non empty numeric string becomes NaN
    console.log(Number(d))//The empty string is 0
    console.log(Number(g))//NaN
    console.log(Number(h))//NaN
    console.log(Number(i))//true is 1
    console.log(Number(j))//false is 0
    console.log(Number(k))//undefined is NaN
    console.log(Number(l))//null is 0
    console.log(Number(m))//The decimal is still the original
    console.log(Number(n))//The empty object is NaN
    console.log(Number(o))//The non empty object is NaN
    console.log(Number(p))//The empty array is 0
    console.log(Number(q))//Non empty array is NaN
    console.log(Number(r))//The number 0 is 0
    console.log(Number(s))//The string 0 is 0

To sum up: the conversion of digital type is still the original. Non pure numeric strings to numbers are NaN. Strings of numeric type will become numbers. true is 1, false is 0, null is 0, undefined is NaN, reference type hollow array is 0, and others are NaN

4, Other types to Boolean types

modeexplainexample
Boolean() functionOther types to Boolean typesBoolean('true')
  • Values representing null and negative will be converted to false, such as' ', 0, NaN, null and undefined
  • The remaining values are converted to true
    boolean conversion
   var a = 1
    var c = 'Finger sucking plain chicken, not chicken legs'
    var d = ''
    var g = 'true'
    var h=  'false'
    var i = true
    var j = false
    var k = undefined
    var l =null
    var m = 1.123
    var n = {}
    var o ={name:"qq"}
    var p =[]
    var q = [1,3,4]
    var r = 0
    var s ='0'
    console.log(Boolean(a))//Non-zero number is true
    console.log(Boolean(c))//Non empty string is true
    console.log(Boolean(d))//Null character is false
    console.log(Boolean(g))//True string is true
    console.log(Boolean(h))//false the string is true
    console.log(Boolean(i))//True of boolean type is true
    console.log(Boolean(j))//False of boolean type is false
    console.log(Boolean(k))//undefined is false
    console.log(Boolean(l))//null is false
    console.log(Boolean(m))//Decimal is also true
    console.log(Boolean(n))//Empty objects are also true
     console.log(Boolean(o))//The object that is not empty is true
    console.log(Boolean(p))//Empty array is true
    console.log(Boolean(q))//Non empty arrays are also true
    console.log(Boolean(r))//The number 0 is false
    console.log(Boolean(s))//The string 0 is also true
    console.log(Boolean(NaN)); // false

To sum up: when converting to boolean, only 0 is false in the number type,
Strings are true,undefined is false,null is false, and reference types are true.
In the judgment statements, a boolean conversion is implicitly carried out. You can see from this.

Tags: Javascript Front-end ECMAScript

Posted on Thu, 04 Nov 2021 19:58:57 -0400 by s1m0n