toString(), toLocaleString() and valueOf Array
The toString() and valueOf() methods return the same value when called on an array.
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var colors = ["red", "blue", "green"]; //creates an array with three strings
document.writeln(colors.toString()); //red,blue,green
document.writeln(colors.valueOf()); //red,blue,green
document.writeln(colors); //red,blue,green
</script>
</head>
<body>
</body>
</html>
document.writeln() calls toString().
toLocaleString() calls each item's toLocaleString() instead of toString() to get its string value.
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var tutorial1 = {
toLocaleString : function () {
return "Java";
},
toString : function() {
return "XML";
}
};
var tutorial2 = {
toLocaleString : function () {
return "HTML";
},
toString : function() {
return "CSS";
}
};
var tutorial = [tutorial1, tutorial2];
document.writeln(tutorial.toLocaleString()); //Java HTML
</script>
</head>
<body>
</body>
</html>
Home
JavaScript Book
Essential Types
JavaScript Book
Essential Types
Array:
- The Array Type
- Array Built-in Methods
- Detecting Arrays
- Get and set array values
- Enumerating the Contents of an Array
- Array Length
- Array join() method
- Array concat()
- Array indexOf()
- Array lastIndexOf()
- Array every()
- Array filter() filters array with the given function.
- Array map()
- Array forEach()
- push() and pop():Array Stack Methods
- push(), shift():Array Queue Methods
- Array reduce()
- Array reduceRight()
- reverse():Reordering array
- Array slice()
- Array some()
- Array splice()
- Array sort()
- toString(), toLocaleString() and valueOf Array
- Array unshift()