An analysis of implicit type conversion in JavaScript

Source: Internet
Author: User
Tags arithmetic numeric string to number

         This article is mainly about the implicit type conversion in JavaScript in the detailed analysis of the introduction, the need for friends can come to the reference, I hope to help you.

If you use a function or method call to explicitly convert a type to another type, called a display transformation, it is called an implicit type conversion. Google and Wikipedia did not find the word "show type conversion", "Implicit type conversion". Call it that.   I. Implicit type conversion in operation     1, "+" operator     code as follows: var a = one, B = ';  ' var c = a + b;    Here the engine will first turn a into a string "11" and then connect to B, and become "1122". Some people will have a question, why not turn B into number 22 and then arithmetic addition, so that C is 33. No why, when the operator "+" on either side is a numeric type, and one is a string type, the JS engine prescribes a string concatenation operation rather than an arithmetic addition. Using the operator "+" feature, it is convenient to convert number to string. If the code is as follows: var a = 11;  alert (typeof a); -->number  A = a + ';  alert (typeof a); -->string    2, the "-" operator   "-" can be either a unary operator (negative) or two-dollar (subtraction). such as the     code below: var a = one, B = ' 5 ';  var c = a-b;  alert (typeof c); --> number    Here in contrast to the above "+", the string B is implicitly converted to the number 5 for arithmetic subtraction. With this feature, it is convenient to convert a string to number copy code code as follows: var a = ' one ';  a = A-';  alert (typeof a);//-->number  &nbsp ; The implicit type conversion   1,if    code exist in the statement are as follows: var obj = {name: ' Jack '}  if (obj) {     //do more  }    This will convert obj implicitly to a Boolean type &nbsP 2,while     Code is as follows: var obj = {name: ' Jack '}  while (obj) {     //do more }    Same An implicit conversion from an identifier to a string occurs when a type conversion in the IF   3,for in time defines an object literal.     Code is as follows: var person = {' name ': ' Jack ', ' age ': 20,school: ' PKU '};  for (var an in person) {      AL ERT (A + ":" + typeof a); }    Here name,age add single/double quotes to emphasize the string type, and the school does not add single/double quotes. We iterate over the object's properties to see its type. The discovery of school was also implicitly converted to a string type. The index of the   array is actually a string type. It's amazing, but it's true. such as     code as follows: var ary = [1,3,5,7];  for (var A in ary) {      alert (A + ":" + typeof a);  The implicit type conversion   code that exists at}    C is as follows: String.prototype.fn = function () {return this};  var a = ' Hello ';  Alert (typeof A.fn ()); -->object  alert (A.fn ()); -->hello    Adds a FN method to the string prototype, which returns this, and we know that this can be understood as an instance object of the current class, and since it is the object then typeof A.fn () naturally returns object. The key is the last alert (A.fn ()), A.fn () returns clearly the object, but implicitly converts it to the string "Hello" display.   The same situation occurs on numeric types, such as     code as follows: Number.protOtype.fn = function () {return this};  var = 10;  alert (typeof A.fn ());//-->object  alert (A.fn ()); -->10    A.fn () returns an object type, but implicitly converts it to a number when alert (A.fn ()).  
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.