toLocaleString() calls each item's toLocaleString() instead of toString() to get its string value in JavaScript

Description

The following code shows how to toLocaleString() calls each item's toLocaleString() instead of toString() to get its string value.

Example


<!--   w  ww . ja  va 2  s  .c  o  m-->
<!DOCTYPE html>
<html>
<head>
<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>

Click to view the demo

The code above generates the following result.

toLocaleString() calls each item