JavaScript array.toLocaleString() function

JavascriptFront End TechnologyObject Oriented Programming

The JavaScript array.toLocaleString() function returns the elements of an array as a string and are separated by a locale specific string such as comma. It can take locale as parameter which specifies the language tag in which the string is to be converted.

Following is the code for the array.toLocaleString() function

Example

 Live Demo

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
   font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.sample {
   font-size: 20px;
   font-weight: 500;
}
</style>
</head>
<body>
<h1>JavaScript array.toLocaleString()</h1>
<div class="sample"></div>
<button class="Btn">CLICK HERE</button>
<h3>Click on the above button to convert the array to UK locale string</h3>
<script>
let fillEle = document.querySelector(".sample");
let arr = [1, 3, new Date()];
fillEle.innerHTML = arr;
document.querySelector(".Btn").addEventListener("click", () => {
   arr = arr.toLocaleString("en-GB");
   fillEle.innerHTML = arr;
});
</script>
</body>
</html>

Output

On clicking the “CLICK HERE” button −

raja
Published on 06-May-2020 12:20:57
Advertisements