The JS string is converted to a number and the number is converted to a string __js

Source: Internet
Author: User
Tags readable

In JavaScript, there are three main ways to convert any value to a string. This article explains each of these methods and their pros and cons.



1. Three methods of converting strings

The three ways to convert value to a string are:



1.value.tostring ()

2. "" + Value

3.String (value)



The problem with the first approach is that it cannot convert null and undefined into strings. There are also the second and third methods, the effect of which is essentially the same.



• "" "+value: Using the addition operator with an empty string can convert any value to a string, and I find the code of this method very readable, but there are some people who prefer to use this type of conversion rather than string (value).

string (Value): This method is more readable, and the only problem is that this function call can confuse some people, especially those familiar with Java, because String is also a constructor. Note that it behaves quite differently as a normal function and as a constructor:


The code is as follows:

> String ("abc") = = = new String ("abc") 
false 

> typeof string ("abc") 
' string ' 
> string (' abc ') instanceof string 
false 

> typeof new String ("abc") 
' object ' 
> New String ("abc") instanceof String 
True





string as a normal function produces a string (a raw value). As a constructor, an instance of a string object is generated. The latter is rarely used in JavaScript, so basically you can ignore string as a constructor, But be sure to remember that it is a conversion function.



2. Nuances of "" "+value and String (value)

By now you know it. Both + and string () can convert their arguments to strings. But there are subtle differences in the way they are converted, but in almost all cases the results are the same.



2.1 Convert the original value to a string

Both of these methods use the ToString () operation inside the engine to convert the original value to a string. " Internal operation means: This operation function is defined in ECMAScript 5.1 (§9.8), but the ES language itself does not have access to it. The following table explains how ToString () transforms the original value.
============

To convert a string to a number, you use the parseint function.

parseint (String): The function resolves from the beginning of a string and returns an integer.

Example: parseint (' 123 '): Return to 123 (int);

parseint (' 1234xxx '): Return to 1234 (int);

If the number is not resolved, the value of a Nan is returned, which can be detected using the isNaN () function;

Example:

var i = parseint (' abc ');

if (isNaN (i))

{

Alert (' NaN value ');

}

The same parsefloat function converts a string into a floating-point number.

Example: parsefloat (' 31.24abc '): return 31.24;

JS number converted to string

Converts a string to a number and uses the ToString method of the string class

Example:

var i = 10;

var s = i.tostring ();

Alert (typeof s); Will output String

the difference between a JS number and a string

JS number of additions and strings of the connection is a + symbol, so whether the addition or string connection depends on the type of variable.

Example:

var a = ' abc ' + ' xyz '; The value of a is: ABCXYZ, the string is connected with the string

var a = 10 + 5; The value of a is: 15, the number is plus

var a = ' abc ' + 10; The value of a is: ABC10, string and number, automatically converts 10 to a string

var a = ' abc ' + + ' CD '; The value of a is: ABC1020CD

var a = + + ' abc ' + ' CD '; The value of a is: 30ABCCD, you can digitally add the first number plus, and then connect

If you want to add a value from an HTML page element, you need to convert to a number first, because the value from the page defaults to the string.


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.