Data type conversion of js note 11

Data type conversion

Convert other data types to number

isNaN , Number , parseInt , parseFloat
In addition, subtraction, multiplication and division

// true         -> 1
// false        -> 0

// null         -> 0
// undefined    -> NaN

// ""           -> 0
// "12"         -> 12
// "12px"       -> NaN/12
// "lilei"      -> NaN
// {}           -> NaN
// function()   -> NaN
// /^$/         -> NaN
// [] -> ""     -> 0
// [12]     -> "12"     -> 12
// [12,23]  -> "12,23"  -> NaN/12

// Convert reference data type to number
// The toString method converts the array to a string, and then the Number method is called to convert the string to a Number

Mathematical operation in js

+- * / addition, subtraction, multiplication and division
In addition to the particularity of addition, other operators are all mathematical operations, that is to say, when encountering non numeric types, you need to convert other operators to number for operation
*Particularity of addition:*
When encountering a string, + is not a mathematical operation, but a string splicing. As long as the string is not encountered, it is a mathematical operation

// Subtraction multiplication and division
// 1 -'1'           -> 0
// 10 * null        -> 0
// 10 / undefined   -> NaN
// 10 * [10]        -> 100

// plus
// 1 + "1"         -> "11"
// null + "1"      -> "null1"

// String concatenation: to convert other values into strings and then concatenate (toString)
// toString of other data types can be wrapped in single (double) quotation marks directly. Only the object has particularity. toString() = "[object object]"

// Practice
// 1+null+true+undefined+[]+'lilei'+null+undefined+[]+10

/* 1 + null         -> 1
 * 1 + true         -> 2
 * 2 + undefined    -> NaN
 * NaN + [] -> NaN + "" -> "NaN"
 * "NaN" + "lilei" -> "NaNlilei"
 * ...
 * "NaNlileinullundefined10"
/

Convert other types to Boolean

Boolean , ! , !!
When the condition is judged, it is also converted to boolean type, and then it is verified that the condition is true or false

Only 0, Nan, empty string, null, undefined are converted to false, and the rest are converted to true

// [] -> true
// -1 -> true

if(box){
    // ->First, get the value stored in the box variable to and convert it to boolean type. If it is true, the condition is true, otherwise it is not
}

if(3 + "3px"){  // "33px"
    // ->Conditions established
}

if(3 - "3px"){  // NaN
    // ->Conditions not established
}

When using = = for comparison

When using = = for comparison, if the data types on the left and right sides are different, the browser will convert to the same type by default, and then compare (= = will not do this)

// ->Object and object: compared with spatial address, not the same space, the result must be false
// [] == [] -> false
// var a = {};
// var b = a;
// a == b -> true

// ->Objects and numbers: converting objects to numbers
// [] == 0 -> true  
// ({}) = = Nan - > false Nan is not equal to itself or any other value

// ->Object and string: convert both sides to numeric comparison
// [] == "" -> true

// ->Objects and Booleans: converting both sides to numbers
// [] == true -> 0 == 1 -> false
// [] == false -> 0 == 0 -> true
// ! [] = = false - > true - >

// ->Strings and numbers: converting strings to numbers

// ->String and Boolean: both converted to numbers

// ->Booleans and numbers: converting Booleans to numbers

Rule: when comparing two equals signs, the types of data values on the left and right sides are different. The browser will convert the types on both sides to numbers and then compare them, except for null and undefined

null == undefined -> true
null === undefined -> false

null as well as undefined Not equal to any other value

  • null == 0 -> false

Tags: REST

Posted on Wed, 18 Mar 2020 09:38:04 -0700 by cybercog