JavaScript quirks 1: implicit type conversion

Source: Internet
Author: User
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 values of other types, but tries to convert them to... SyntaxHighlighter. all ();

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. These values are eventually converted to true or false.

The following values are converted to false:

Undefined, null
Boolean: false
Number:-0, + 0, NaN
String :''
All other values are considered to be true. The value converted to 'false' is falsy, and the value converted to '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 seemingly false values. If the value is converted to a string, the value is 'true '.

For example, false.

> Boolean (false)
False
> String (false)
'False'
> Boolean ('false ')//!!
True
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 the 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 try toString () for conversion, if not the original value, then try valueOf ().

4. Reading
In JavaScript, how much is {} + {} equal?
JavaScript: Convert all values into objects
Why ++ [[] [+ [] + [+ [] = 10?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.