JavaScript join() Method: Array Object
Description
The join() method is used to join all elements of an array into a string. The elements are separated by a specified separator.
Version
Implemented in JavaScript 1.1
Syntax
join(separator)
Parameters
separator: Specifies a string to separate each element of the array. Default separator for the join() method is comma(',').
Example:
In the following web document there is an array called fruits with four elements, join() method is used in different ways : using the default separator, then a comma with a space separator, and then a hyphen separator.
HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf8" />
<title>JavaScript join() method Example-1</title>
<style type="text/css">
h1 {color:red}
</style>
</head>
<body>
<h1>JavaScript : join() method</h1>
<script src="array-join-example1.js">
</script>
</body>
</html>
JS Code
var fruits = new Array("Orange", "Apple", "Banana", "Chery");
var fruitslist1=fruits.join();
var newParagraph = document.createElement("p");
var newText = document.createTextNode("Fruits List : " + fruitslist1 );
newParagraph.appendChild(newText);
document.body.appendChild(newParagraph);
var fruitslist2=fruits.join(', ');
var newParagraph1 = document.createElement("p");
var newText1 = document.createTextNode("Fruits List : " + fruitslist2);
newParagraph1.appendChild(newText1);
document.body.appendChild(newParagraph1);
var fruitslist3=fruits.join('-');
var newParagraph2 = document.createElement("p");
var newText2 = document.createTextNode("Fruits List : " + fruitslist3);
newParagraph2.appendChild(newText2);
document.body.appendChild(newParagraph2);
View the example in the browser
Practice the example online
JavaScript join() method Example-1See also :
JavaScript Core objects, methods, properties.
Previous: JavaScript concat() Method : Array Object
Next:
JavaScript pop() Method: Array Object
Test your Programming skills with w3resource's quiz.
JavaScript: Tips of the Day
Returns the difference (in days) between two dates
Example:
const tips_DiffBetweenDates = (dateInitial, dateFinal) => (dateFinal - dateInitial) / (1000 * 3600 * 24); console.log(tips_DiffBetweenDates(new Date('2020-05-13'), new Date('2020-06-05')));
Output:
23
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework