Implicit conversion of JavaScript

Source: Internet
Author: User
Tags string to number

The data types of JavaScript are divided into six kinds, namely null,undefined,boolean,string,number,object. object is a reference type, and the other five types are basic or primitive. We can use the TypeOf method to print out which type it belongs to. Different types of variables are compared to the first type, which is called type conversion, and the type conversion is also called an implicit conversion. Implicit conversions typically occur in operator subtraction, equal to, and less than, greater than, and so on.

typeof ' One '  //string       typeof(one) //number' one ' < 4     //False 
conversion of basic types

Let's talk about subtraction first:

1. String plus number, the number will be converted to a string.

2. Number minus string, string converted to number. If the string is not a pure number, it will be converted to Nan. The same is true for strings minus numbers. Two string subtraction is also first converted to a number.

3. Multiplication, except, greater than, less than the same as the conversion of the minus.

//implicit conversion +-* = =/// + 10 + ' 20 '// .// -10-' 20 '//-10Ten-' One '//NaNTen-' 100a '//NaN// *10* ' 20 '// $' 10 ' * ' 20 '// $// /20/' 10 '//2'20 '/' 10 '//2''/' One '//nan

Let's look at a group of = =.

1.undefined equals null

2. String to numeric when comparing strings and numbers

3. When the number is a Boolean comparison, the Boolean-to-digital

4. String and Boolean comparison, the two go to the number

//  == null;    // true' 0 ' = = 0;  // True, string to number false;           // True, Boolean to digital false;    // true, the two go to numbers NULL false;     // false  false; // false     
conversions of reference types

Comparisons between basic types are relatively straightforward. The comparison of reference types and basic types is relatively complex, first to convert the reference type to the basic type, and then compare the above method. The reference-type-to-Boolean is all true. For example, an empty array, as long as the object is a reference type, so [] is true. A reference type to a number or a string is either valueof () or ToString (), the object itself inherits Valuof () and ToString (), and valueof () and ToString () can be customized. Depending on the object, the inherited valueof () is converted to a string, a number, or itself, and the object must be converted to a string using ToString. The generic object calls valueof () by default.

1. When the object goes to a number, call valueof ();

2. Call ToString () when the object goes to a string;

Let's take a look at the following example:

0 = = [];        // true, 0 = = [].valueof (); 0 = = 0;' 0 ' = = [];      // false, ' 0 ' = = [].tostring (); ' 0 ' = = '; 2 = = [' 2 '];     // true, 2 = = [' 2 '].valueof (); 2 = = ' 2 ', 2 = = 2;' 2 ' = = [2];     // true, ' 2 ' = = [2].tostring (); ' 2 ' = = ' 2 '; == ! [];      // true, [].valueof () = =! Boolean ([]), 0 = = False, 0 = = 0;

When the object is converted to a number, call valueof (), which is called ToString () before that, so I guess the valueof method is like this. So the example above 0 = = [] to be changed to the following more reasonable. In any case, [] Finally, it turns into 0.

var function () {    varthis. toString ();    // First Call ToString () and turn to string    // ... }0 = = [];        // true, 0 = = [].valueof (); 0 = = ' 0 ', 0 = = 0;

Custom valueof () and ToString ();

1. The custom valueof () and ToString () are present and will be called valueof () by default;

2. If only ToString (), call ToString ();

var a = [1functionreturn 1functionreturn ' 1 '  + 1;         // 2, ValueOf () First Call

removing valueof () will call ToString ().

var a = [1functionreturn 1functionreturn ' 1 '  + 1;         // 2, First call valueof () // Remove valueof Delete  + 1;        // ' 11 ', call ToString ()

What happens if we go back?

var a = [1function () {returnfunction () {return 1 ;}; 1-a;        // NaN

Other objects Call valueof () to a different type:

var a = {};a.valueof ();     // Object {} var a = [];a.valueof ();     // []    itself varnew  Date (); a.valueof ();     // 1423812036234  Digital varnew  RegExp (); a.valueof ();     //     /(?:) /  Regular Object

Comparisons between reference types are memory address comparisons and do not need to be implicitly converted, as there is not much to say.

[] = = []  //false address is not the same as var a === = a   //true

an explicit conversion

Explicit conversions are straightforward and can be directly converted using classes as methods.

Number ([]);        // 0 String ([]);        // "' Boolean ([]);       // true

There is also a simpler way to convert.

3 + '    / / string ' 3 '+ ' 3 '      //  number 3! ' 3 '     //  true

Resources:

<<javascript Authoritative Guide Sixth edition >> chapter III

http://www.cnblogs.com/snandy/archive/2011/03/18/1987940.htmlhttp://www.2cto.com/kf/201304/204383.htmlhttp:// 1.liangtao.sinaapp.com/?p=555

Implicit conversion of JavaScript

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.