What about data type conversion in JavaScript?

Source: Internet
Author: User

In JS, data types are generally divided into two categories: Original Value Type and reference type. The original value types include:String),Number),Boolean)And two special values.NullAndUndefined. The reference type mainly refers to the object. In JS, the object has a wide range, and even in JS, everything is like an object. Objects can be dividedObject)And more specificArray),Function). In JavaScript, the typeof operator only returns the following types: sting, number, Boolean, object, function, and undefined. If typeof is null, the object is returned, however, null is generally considered not an object.

The statement about whether everything in Javascript is an object is controversial. For example, we can call the method directly on the string literal defined by single quotes or double quotes:

 
'This is a string '. charat (0 );

This statement can be executed and get the correct result. A string is indeed an object, but the problem is that a string that uses a literal instead of a constructor is not an object? This statement is not just a method called in the string literal, but an object that can call the method. Yes. What we see is true, but what is the truth?

The fact is that a string defined by literal is not an object, or it can only be said to be a pseudo object at most, because when a method or attribute is called in the string literal,ProgramIt will silently create a temporary String object that is equal to the literal value, then call the attribute or method on the temporary object, and finally return the result, this temporary String object will be destroyed after the mission is completed.

There is also strong evidence that you can add an attribute on the string literal or change an attribute value, but the result cannot be successful:

 
VaR S = 'this is a string '; s. bar = 'hello'; // Add an attribute alert (S. bar); // The result is undefined, not 'hello'

The reason for undefined is: S. the bar attribute is added to the temporary String object secretly created by the program, but we cannot get reference to this temporary character creation object, and the temporary String object will soon be destroyed. Therefore, adding an attribute to a string literal or changing the attribute value does not make any sense, because it will not be affected at all.

The number literal and Boolean literal are similar principles, so we will not go into detail here.

Next let's go to the topic-conversion between various data types in Js.

1. convert to a Boolean Value

The program is in the IF statement and | ,&&,! The expression is automatically converted to a Boolean value in a logical judgment environment.

There are two ways to manually convert something to a Boolean value: 1. Use !! 2. Use Boolean (). Remember not to add new before;

1. convert a number to a Boolean Value

Except that 0 is converted to false, all self-contained numbers are converted to true, and Nan is always converted to false.

2. convert a string to a Boolean Value

This is simpler. Except for converting a null string to false, all strings are converted to true.

3. convert other types to boolean values

Undefined and null will be converted to false, and any object (including arrays) and function will be converted to true. Remember, it is any

 
VaR o = new Boolean (false); alert (o); // convert to a string and the result is falsealert (!! O); // convert to a Boolean value and the result is true.

 

Ii. convert to a string

There are two ways to forcibly convert something into a string:

''+ X // method 1. Use an empty string to add string (x) // method 2. Use a string constructor without new.

1. convert numbers into strings

There is nothing to say. Numbers are converted to strings as they are, but they are represented by scientific notation (that is, numbers with E) it is converted into a string of real numbers represented internally.

Note that when the binary plus sign operator is used, if one of the two arithmetic numbers is not a number, the string join operation is performed instead of the mathematical addition operation, both operations are converted into strings. When null is added to a number, it is not connected to a string. Instead, it is converted to 0 to form a mathematical element.

 
[] + 1 + 3 // The result is 13 [1] + 3 // The result is 13 null + 1 + 3 // The result is 4

2. convert other types to strings

When an object or function is converted to a string, its tostring () method is called for conversion. The default value is object. prototype. tostring and function. prototype. tostring, but they can be overwritten by our custom tostring method. When a function is converted into a string, the function'sSource codeThe result of function. Prototype. tostring method depends on how the Environment implements it.

 

Iii. Convert to numbers

All mathematical operators except the plus sign are converted to numbers. There are two ways to forcibly convert a thing into a number:

 
+ X // use a mona1 plus operator, which is the fastest way to x-0 or x * 1 // another form of number (X) // use a number constructor without new to convert the data.

1. convert a string to a number

Except for the Null String, it is converted to 0. If the string is in the correct numeric writing format, it can be converted to the corresponding number, decimal, scientific count, octal, and hexadecimal form. However, if it contains other things that cannot constitute a number or do not conform to the number Writing rules, it will be converted to Nan.

Nan means that it is not a number. The result of any number calculation with Nan is Nan, and Nan is not even the same as itself.

2. convert other types to numbers

Objects and functions are always converted to Nan, and undefined is also converted to Nan, but null is converted to 0.

The preceding table misses the array. The array is first converted to a string and then converted to a number.

 
Alert (+ []); // The result is 0 alert (+ [1]); // The result is 1 alert (+ [1, 2]); // The result is nanalert (+ new array (1); // The result is 0 alert (+ new array (1, 2); // The result is Nan

3. Two numeric conversion functions

Parseint

Parsefloat

Data type conversion seems simple, but some things are easy to confuse. It still takes some effort to grasp it.

References: http://jibbering.com/faq/notes/type-conversion/

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.