This article mainly shares with you JS data type conversion examples, hoping to help everyone.
Convert other data types to number type
true -> 1 false -> 0'' -> 0'1' -> 1'12px' -> true/12/NaNnull -> 0undefined -> NaN{} /^$/ function() {} -> NaN[] -> 0[12,23] -> NaN//=> 通过toStirng把数组转换为字符串<br> // 然后Number把字符串转换为数字
Mathematical operations in js
number type, and then calculate
1-'1' -> 010 * null -> 010 / undefined -> NaN10 * [10] -> 100
1+'1' -> '11'null+'1' -> 'null1'//=>把其他值先转换为字符串再进行拼接对象.toString() -> '[object object]'
Thinking1+null+true+undefined+[]+'xieth'+null+undefined+[]+10Copy after loginCopy after login
Result"NaNxiethnullundefined10"Number(undefined) -> NaN//Number在进行转换的时候,只要包含非有效数字,返回都为NaNCopy after login
Convert other types to Boolean types
Only 0 NaN null undefined empty string-> Convert to falseConversion of data types in jsBasic data typesThe rest are true
Convert other data types to number type
true -> 1 false -> 0'' -> 0'1' -> 1'12px' -> true/12/NaNnull -> 0undefined -> NaN{} /^$/ function(){} -> NaN[] -> 0[12,23] -> NaN//=> 通过toStirng把数组转换为字符串<br> // 然后Number把字符串转换为数字
Mathematical operations in js
number type, and then perform the calculation
1-'1' -> 010 * null -> 010 / undefined -> NaN10 * [10] -> 100
1+'1' -> '11'null+'1' -> 'null1'//=>把其他值先转换为字符串再进行拼接对象.toString() -> '[object object]'
Thinking1+null+true+undefined+[]+'xieth'+null+undefined+[]+10Copy after loginCopy after login
Result"NaNxiethnullundefined10"Number(undefined) -> NaN//Number在进行转换的时候,只要包含非有效数字,返回都为NaNCopy after login
Convert other types to Boolean types
Only 0 NaN null undefined empty string -> Convert to falseRelated recommendations:The rest are true
Detailed explanation of php constants and data type instances
Detailed explanation of special data types in JavaScript
The above is the detailed content of js data type conversion example. For more information, please follow other related articles on the PHP Chinese website!