Javascript 1: implicit type conversion

Source: Internet
Author: User

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:ifStatement), any value can be used.
Eventually these values will be convertedtrueOrfalse.

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:

  1. First, the system does not have any warning.
  2. 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 seeminglyfalseIf 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:

  1. CallvalueOf(). If the result is a raw value (not an object), convert it to a number.
  2. Otherwise, calltoString()Method. If the result is an original value, convert it to a number.
  3. 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
  1. In JavaScript, how much is {} + {} equal?
  2. Javascript: Convert all values into objects
  3. 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.