Home >Web Front-end >JS Tutorial >Javascript conversion method for operations between different types of data_javascript skills

Javascript conversion method for operations between different types of data_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:00:06942browse

Different types of basic data in js can be converted. This conversion is rule-based and not random. There are 5 basic types of data in js: string, number, boolean, null, and undefined. Among them, the first three types are commonly used for calculation or comparison.

Conversion between basic data

其它类型转化数字
原始数据类型 目标类型Number
undefined NaN
null 0
false 0
true 1
数字串 相应的数字
不能转化的字符串 NaN
其它类型转化为字符串
原始数据类型 目标类型String
undefined undefined
null null
false false
true true
数字 数字字符串

Addition between different types of basic data, the data is first converted to number, and then converted to string (if string type data is involved in the operation)

Copy code The code is as follows:

null undefined // 0 NaN

null false // 0 0

1 true // 1 1

1 '1' //'11'; The result of adding numbers and strings is string

1 2 '3' //'33'; The result of (1 2) is then added to '3'; here each addition step must be taken apart separately, otherwise it will become the same result as below.

1 (2 '3') //'123'; first calculate 2 '3', then 1 '23'

's' null //'snull'

' s' undefined // 'sundefined'

's' true //'strue'

1 true undefined 's' // 1 1 NaN 's'=2 NaN 's'= NaN 's'=NaNs

Objects participate in addition and subtraction operations

Objects participate in basic type data operations and are first converted into basic types. First call its valueOf method. If the returned value is not a basic type, then call its toString method. If the returned value is not a basic type, an error will be thrown. However, Date data is just the opposite

Copy code The code is as follows:

//In order to facilitate observation, rewrite the toString method of Date and valueOf method

Date.prototype.toString = function(){
return 1;
}

Date.prototype.valueOf = function(){

return 2;
}

var a = new Date,
b = new Date;

a b; // Call toString, 1 1, the result is 2

// Rewrite the toString method

Date.prototype.toString = function(){

return {};
}

var c = new Date ,
d = new Date;

c d; // Calling the toString method returns not the basic type, then calling valueOf, 2 2, the result is 4

// Then rewrite valueOf Method
Date.prototype.valueOf = function(){

return {};
}

var e = new Date,
f = new Date;

e f; // Error report

Replace the above example with Object or other types to get the corresponding results, but call valueOf first and then call toString.

The magical effect of ' '

There is a plus sign ‘ ‘ in front of the data, which can convert the string into a number

Copy code The code is as follows:

'1' 1 // 2

's' 2 // NaN

Note: This is the first time, the format is not good and there are many mistakes, everyone is welcome to contribute
Statement:
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