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 ()
- 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.
- The ToString () method gets a string (traditional string), and the toLocaleString () method gets
is the localestring (local environment string).
- If you are developing a script that is used worldwide, then convert the object to a string using the
ToString () method to complete.
- 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.
- 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