I understand the implicit type conversion of JavaScript.

Source: Internet
Author: User

Type conversions in JavaScript can be classified as implicit type conversions and explicit type conversions. The dominant transformation is to change the data type directly in the code by means of the method. The common methods are tostring,tovalue,tonumber,toboolean and so on. The implicit type is that the program performs the type conversion automatically when it executes.

The most common operator for implicit type conversion in javascript is = =, and of course operator + also makes implicit type conversions.

Here are a few examples.

Console.log (undefined== ' undefined ');

Console.log (Undefined==null);

Console.log (true==2);

Console.log ({}=={});

The first print is false, the second one prints true, the third one prints false, and the fourth one prints false.

The first false is because undefined does not perform implicit type conversions.

The first true, although undefined does not perform implicit type conversions, but null and undefined are equal;

The second false is because true converts to 1 instead of 2 to true. This problem is believed to be encountered by most beginners.

The fourth false is because comparisons between objects are comparisons between comparison references. The first {} object is different from the second {} object address.

Summary below: = = operator

1. See if the operand has a Boolean type, if one has first changed the Boolean type to number type.

2. See if the operand has no string type, and if two are string types, compare directly; if one is a string, one is an object, the object goes to a string and then a string is compared;

3. See if the operand has no object type, and if two objects are directly compared, and if one is a value, the object goes to the numeric value and then compares it.

4.null,undefined do not perform type conversions, but null,undefined are equal.

I understand the implicit type conversion of JavaScript.

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.