<>javascript Type conversion in

javascript It's a weakly typed language , Variables are type independent , So sometimes we need to do type conversion

<> one , Digital conversion (number)

Two methods :
number: What is the way of type transformation v8 Transformation rules of the lowest mechanism of engine parseFloat,parseInt: Is to provide additional method conversion rules
<>number

First, convert the reference type to a string (tostring) method , When converting a string to a number
character string => number : Invalid number converted to string Boolean value => number :1 or 0 null => number : 0; underfined => number :NaN, ‘
’ => number :0 object => number : First, convert the object to a string , It's turning into numbers 【】 => number :0 Number(10); // 10
Number('10'); // 10 Number(null); // 0 Number(''); // 0 Number(true); // 1
Number(false); // 0 Number([]); // 0 Number([1,2]); // NaN Number('10a'); //
NaN Number(undefined); // NaN
parseint()
parsefloat(【va】,【 Base system 】), Search the string from left to right for valid numeric characters , Knew that an invalid string was encountered , Stop searching , Return what you find as a number ,

If it's not a string , First convert to string before using this method
let str = '12.5px' parseInt(str) // 12 parseFloat(str)// 12.5
parseFloat(true)// NaN
isNaN Judge number type :

If the current type is numeric , return false, Otherwise, return true

<> two , String type conversion (string)

Primitive type , use tostring method () That's what it was like before Put quotation marks on the outside
number => character string : Wrap a layer of quotation marks . NaN => character string : 'NaN' . true => character string : 'true' . null => character string :
'null' ( The browser will report an error ( You are not allowed to use it )—— Conversion is usually possible ) undefined => character string :'undefined' ( The browser will report an error ( You are not allowed to use it )——
Conversion is usually possible ) Object => character string : '[object,Object]' .
The result of ordinary object transformation is “ [object,object] ”, because Object.prototype.toString
Method is not converted to a string , It's used to detect data types .
String(123); // "123" String(true); // "true" String(null); // "null"( report errors )
String(undefined);// "undefined"( report errors ) String([1,2,3]) // "1,2,3" String({}); //
"[object Object]"
<> three , Boolean type conversion (boolean)

1:‘ ’,

2:underfined,

3:NaN,

4:null,

5:false,

6: 0 ,

above 6 The value is null when converted to a Boolean value false, Other types are true
Boolean('') // false Boolean(undefined) // false Boolean(null) // false
Boolean(NaN) // false Boolean(false) // false Boolean(0) // false Boolean({})
// true Boolean([]) // true
<> four , Conversion of primitive type

There are two types of transformation : Convert to string type or other primitive type .

If it is already the original type , No more conversion is required .

If converted to string type , Call the toString() method .

If it is other basic type , Call the valueOf() method .

If the return is not the original type , The call continues toString() method .

If the original type has not been returned , An error is reported .

Technology
©2019-2020 Toolsou All rights reserved,
Hikvision - Embedded software written test questions C Language application 0 The length of array in memory and structure is 0 In depth analysis data structure --- The preorder of binary tree , Middle order , Subsequent traversal How to do it ipad Transfer of medium and super large files to computer elementui Shuttle box el-transfer Display list content text too long 2019 The 10th Blue Bridge Cup C/C++ A Summary after the National Games ( Beijing Tourism summary )unity Shooting games , Implementation of first person camera python of numpy Module detailed explanation and application case Study notes 【STM32】 Digital steering gear Horizontal and vertical linkage pan tilt Vue Used in Element Open for the first time el-dialog Solution for not getting element