The join()
method returns the array as a string.
The elements will be separated by a specified separator. The default separator is comma (,).
Note: this method will not change the original array.
Syntax and Usage
array.join(separator)
Parameter Values
separator -> Optional. The separator to be used. If omitted, the elements are separated with a comma
Example(1)
1.Create array
var flowers = ["Lily", "Rose", "Lotus", "Jasmine"];
2.how to use join()
method
var x = document.getElementById("demo");
x.innerHTML = flowers.join();
Example(2)
function joinfunction() {
var flowers = ["Lily", "Rose", "Lotus", "Jasmine"];
var x = document.getElementById("demo");
x.innerHTML = flowers.join('-');
}
Complete code for Array Join Method In JavaScript
<!DOCTYPE html>
<html>
<head>
<title>How Can I Use Array Join Method In JavaScript With Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<style>
h3{
color:red;
}
</style>
<body>
<div class="container">
<br>
<br>
<div class="text-center">
<h1>Use Array Join Method In JavaScript </h1>
</div>
<br>
<div class="well">
<button class="btn btn-primary" onclick="joinfunction()">Click it</button>
<h1 id="demo"></h1>
</div>
<br>
</div>
</body>
</html>
<script>
function joinfunction() {
var flowers = ["Lily", "Rose", "Lotus", "Jasmine"];
var x = document.getElementById("demo");
x.innerHTML = flowers.join();
}
</script>