Home > Web Front-end > JS Tutorial > body text

js data type conversion example

小云云
Release: 2018-03-10 15:11:27
Original
1295 people have browsed it

This article mainly shares with you JS data type conversion examples, hoping to help everyone.

Basic data types

Convert other data types to number type

  • ##isNaN, Number, parseInt, parseFloat

  • When performing mathematical operations of addition, subtraction, multiplication and division

true -> 1  false -> 0'' ->
 0'1' -> 1'12px' -> true/12/NaNnull ->
  0undefined -> NaN{} /^$/ function()
  {} -> NaN[] -> 0[12,23] -> NaN//=>
   通过toStirng把数组转换为字符串<br>
   // 然后Number把字符串转换为数字
Copy after login

Mathematical operations in js

  • +, -, *, / Addition, subtraction, multiplication and division

  • Except for the addition operation, which has special features, the rest of the operators are mathematical operations, that is, when encountering Non-numeric types, first convert to

    number type, and then calculate

1-'1' -> 010 * null -> 010 / undefined -> NaN10 * [10] -> 100
Copy after login
Copy after login
  • Speciality of addition: When encountering a string, + is String concatenation is a mathematical operation without encountering a string

1+'1' -> '11'null+'1' -> 'null1'//=>把其他值先转换为字符串再进行拼接对象.toString() -> '[object object]'
Copy after login
Copy after login
Thinking

1+null+true+undefined+[]+'xieth'+null+undefined+[]+10
Copy after login
Copy after login
Result

"NaNxiethnullundefined10"Number(undefined) -> 
NaN//Number在进行转换的时候,只要包含非有效数字,返回都为NaN
Copy after login
Convert other types to Boolean types

  • Boolean,!,!!

  • When judging conditions , also converted to Boolean type, and then verify the true or false condition

Only 0 NaN null undefined empty string-> Convert to false

The rest are true

Conversion of data types in js

Basic data types
Convert other data types to number type

  • isNaN, Number, parseInt, parseFloat

  • When performing mathematical operations of addition, subtraction, multiplication and division

true -> 1  false -> 0'' -> 0'1' -> 1'12px' -> 
true/12/NaNnull -> 0undefined -> NaN{} 
/^$/ function(){} -> NaN[] -> 0[12,23] -> 
NaN//=> 通过toStirng把数组转换为字符串<br>
// 然后Number把字符串转换为数字
Copy after login

Mathematical operations in js

  • +, -, *, / Addition, subtraction, multiplication and division

  • In addition to addition operations, there are special features , the rest of the operators are all mathematical operations, that is, when encountering a non-numeric type, first convert it to the

    number type, and then perform the calculation

1-'1' -> 010 * null -> 010 / undefined -> NaN10 * [10] -> 100
Copy after login
Copy after login
  • addition The particularity: when encountering a string, + is string concatenation, and when not encountering a string, it is a mathematical operation

1+'1' -> '11'null+'1' -> 'null1'//=>把其他值先转换为字符串再进行拼接对象.toString() -> '[object object]'
Copy after login
Copy after login
Thinking

1+null+true+undefined+[]+'xieth'+null+undefined+[]+10
Copy after login
Copy after login
Result

"NaNxiethnullundefined10"Number(undefined) -> NaN//Number在进行转换的时候,只要包含非有效数字,返回都为NaN
Copy after login
Convert other types to Boolean types

  • Boolean,!,!!

  • When judging the condition, it is also converted to Boolean type, and then the true or false condition is verified.

Only 0 NaN null undefined empty string -> Convert to false

The rest are true

Related recommendations:

JavaScript Basics Data Type

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!