The difference between ToString (), tolocalestring (), valueOf () in JS

Source: Internet
Author: User

Objective

An Array, Boolean, Date, number, and other objects all have
ToString (), tolocalestring (), ValueOf () Three methods, what is the difference between these three methods?

One, JS Array

1) Example

var array = new Array("niu","li","na");  console.log(array.valueOf());console.log(array.toString());console.log(array.toLocaleString());

2) Results

3) Summary
ValueOf: Returns the array itself
ToString (): Converts the array to a string and returns the result, with each item separated by commas.
Tolocalstring (): Converts the array to a local array and returns the result.

Second, JS Boolean

1) Example

var boolean = new Boolean();console.log(boolean.valueOf());console.log(boolean.toString());

2) Results
Fasle,fasle
3) Summary
ValueOf: Returns the original value of the Boolean object.
ToString (): Returns the string "true" or "false" based on the original Boolean value or the value of the Booleanobject object. The default is "false".
Tolocalstring (): The Boolean object does not have a tolocalstring () method. However, using this method on a Boolean object does not have an error.

Third, JS Date

1) Example

var date = new Date();console.log(date.valueOf());console.log(date.toString());console.log(date.toLocaleString());

2) Results

3) Summary
ValueOf: Returns the original value of the Date object, expressed in milliseconds.
ToString (): Converts the Date object to a string and returns the result. expressed using local time.
Tolocalstring (): Converts a Date object to a string based on local time, and returns the result, formatted according to local rules for the returned string.

Four, JS Math

1) Example

console.log(Math.PI.valueOf());

2) Results

3) Summary
ValueOf: Returns the original value of the Math object.

Five, JS number

1) Example

var num = new Number(1337);console.log(num.valueOf());console.log(num.toString());console.log(num.toLocaleString());

2) Results

3) Summary
ValueOf: Returns the base numeric value of a number object.
ToString (): Converts the number to a string, using the specified cardinality.
Tolocalstring (): Converts a number to a string, using the local number format order.

Six, JS String

1) Example

var string = new String("abc");console.log(string.valueOf());console.log(string.toString());

2) Results
Abc
Abc
3) Summary
ValueOf: Returns the original value of a string object.
ToString (): Returns a string.

Vii. toString () VS tolocalstring ()
    1. Tolocalstring () is the toLocaleString () method that invokes each array element, and then uses the
      A locale-specific delimiter connects the resulting string to form a string.
    2. The ToString () method gets a string (traditional string), and the toLocaleString () method gets
      is the localestring (local environment string).
    3. If you are developing a script that is used worldwide, then convert the object to a string using the
      ToString () method to complete.
    4. Localestring () returns a string based on the local environment of your machine, and it is returned by ToString ().
      The symbols used in different local environments will have subtle changes in values.
    5. So using ToString () is an insurance method that returns a unique value, which is not due to changes in the local environment
      Changed. It is recommended to use localestring () if it is to return data of a time type. If in the background
      Handle the string, be sure to use ToString ().

The difference between ToString (), tolocalestring (), valueOf () in JS

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.