Data type conversion in a few easy to mistake the point!

parseIntA method for a string into an integer, if parseIntthe parameter is not a string, then it will first convert it to a string.

parseInt(1.23) // 1 //

It is equivalent to the parseInt ( '1.23') //. 1;

If the first character of the string can not be converted to digital ( followed except the sign of the number ), and returnsNaN;

ParseFloat: ditto

 

Of particular note:parseFloat it will be converted to an empty string NaN.

 

These features make the parseFloatconversion result is different from the Numberfunction.

Example: parseFloat ( True) // NaN

Number(true) // 1

parseFloat(null) // NaN

Number(null) // 0

parseFloat ( '') // NaN

Number('') // 0

parseFloat('123.45#') // 123.45

Number ( '123.45 #') // NaN

Index assignment:

var x = 2;

var y = 3;

** x = y;

console.log(x); // 8

// equivalent x = x ** y

 

Cast:Number() , String()andBoolean()

Number parameter is an object method, returns NaN, unless array comprising individual values.

Number({a: 1}) // NaN

Number ([ 1, 2, 3]) // NaN

Number([5]) // 5

StringIf the parameter is an object method, it returns a string type; if the array, return the string array.

String({a: 1}) // "[object Object]"

String([1, 2, 3]) // "1,2,3"

 

Boolean()

Its conversion rules are relatively simple: In addition to the conversion value is the result of five false, all other values true.

undefined

null

0(Contain -0and +0)

NaN

''(Null string)

 

Implicit type conversion

+

The first case, each of different types of data operations.

123 + 'abc' // "123abc"

The second case, seeking Boolean value of a non-boolean types of data.

if ('abc') {
  console.log('hello') }  

The third case, the use of unary operator (i.e., the values of non-numeric type +and -).

var a = "10";
console.log(+ a,typeof +a); // 10 "number" + {foo: 'bar'} // NaN - [1, 2, 3] // NaN
自动转换的规则是这样的:预期什么类型的值,就调用该类型的转换函数。比如,某个位置预期为字符串,就调用String函数进行转换。如果该位置即可以是字符串,也可能是数值,那么默认转为数值。

自动转换为布尔值

ifCondition part of the statement), non-boolean parameter will automatically be converted to a Boolean value. Internal system will automatically call the Booleanfunction.

Therefore, except for the following five values, others are automatically converted true.

  • undefined
  • null
  • +0or-0
  • NaN
  • ''(Null string)

Automatically converted to a string:

 

JavaScript is expected to meet local string, the string will be automatically converted to a non-string value. Specific rules, the first type of complex values ​​into values ​​of the original type, then the value into a string of primitives.

 

Automatic string conversion mainly occurs when adding a string. When a string value, the other value is not a string, the string into the latter.

 

'5' + 1 // '51'
'5' + true // "5true" '5' + false // "5false" '5' + {} // "5[object Object]" '5' + [] // "5" '5' + function (){} // "5function (){}" '5' + undefined // "5undefined" '5' + null // "5null"

自动转换为数值

JavaScript is expected to meet local values, it will be automatically converted into a numerical parameter values. Internal system will automatically call the Numberfunction.

In addition addition operator ( +) it is possible to turn the string operator, other operators are automatically converted into the numeric operator.

'5' - '2' // 3
'5' * '2' // 10 true - 1 // 0 false - 1 // -1 '1' - 1 // 0 '5' * [] // 0 false / '5' // 0 'abc' - 1 // NaN null + 1 // 1 undefined + 1 // NaN
注意:null转为数值时为0,而undefined转为数值时为NaN
 

 

Guess you like

Origin www.cnblogs.com/hy96/p/11372128.html