Original: javascript quirk 1: implicit conversion of Values
Translation: javascript quirks 1: implicit type conversion
Translator: justjavac
Zero: Abstract
[This post is the first in the 12 quirks series of JavaScript.]
Javascript is very tolerant. "Don't refuse", don't care what type.
For example, if it wants to accept numbers, it does not reject other types of values, but tries to convert them into numbers:
> '5' - '2'3> '5' * '2'10
Automatic conversion to a Boolean value usually does not cause problems and is often useful. By @ justjavac ). Even so, these implicit conversions can also lead to quirks ). However, when the string is automatically converted, it may cause problems.
1. implicit conversion to boolean: "truthy" and "falsy"
When JavaScript requires a Boolean value (for example:if
Statement), any value can be used.
Eventually these values will be convertedtrue
Orfalse
.
The following values are convertedfalse
:
- Undefined, null
- Boolean: false
- Number:-0, + 0, Nan
- String :''
All other values are consideredtrue
. The value converted to 'false' is falsy and converted
The value of 'true' is truthy. You can use Boolean to test the conversion of a value.
Boolean ):
> Boolean(undefined)false> Boolean(0)false> Boolean(3)true
Ii. implicit conversion of strings
In web development, we often get string values. What we expect is actually numbers or boolean values. For example, data in a user input form. If you forget to explicitly convert these strings, JavaScript will surprise you, mainly reflected in two aspects:
- First, the system does not have any warning.
- Second, these values are automatically converted, but they are indeed incorrect.
For example, the addition operator (+) has this problem, because ** as long as one of the operands is a string, it performs the join string operation (instead of the addition operation, even if they are numbers )**.
In the following JavaScript code, we originally expected to add 1 and 5. However, we use strings '5' and '1 '.
> Var x = '5'; // incorrect hypothesis: X is a number> x + 1 '51'
In addition, there are some seeminglyfalse
If it is converted to a string, it becomes 'true '.
For example:false
.
> Boolean(false)false> String(false)'false'> Boolean('false') // !!true
For example:undefined
.
> Boolean(undefined)false> String(undefined)'undefined'> Boolean('undefined') // !!true
Iii. implicit conversion of Objects
Objects are implicitly converted only when a number or string is required in a javascript expression or statement. To convert an object to a number, perform the following three steps:
- Call
valueOf()
. If the result is a raw value (not an object), convert it to a number.
- Otherwise, call
toString()
Method. If the result is an original value, convert it to a number.
- Otherwise, a type error is thrown.
Step 1:
> 3 * { valueOf: function () { return 5 } }15
Step 3:
> function returnObject() { return {} }> 3 * { valueOf: returnObject, toString: returnObject }TypeError: Cannot convert object to primitive value
If you convert an object to a string, the first and second steps of the conversion operation will be changed: First trytoString()
Conversion. If it is not the original value, try againvalueOf()
.
4. Reading
- In JavaScript, how much is {} + {} equal?
- Javascript: Convert all values into objects
- Why ++ [[] [+ [] + [+ [] =
10?