- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
The join() method takes as input a single separator string and returns a string with all the elements of the array separated by the specified separator string. The separator string by default is the comma(,).
The join method uses the toString() method to convert the elements of the array into corresponding strings. A null or undefined value is converted to an empty string.
var str = arr.join(separator)
Here we are going to show the use of join method. We will create an array of strings and pass it as a function argument for printing it also providing the separator string. Let’s look at the code for same.
In the above code, every array element is separated by a newline character. Note however that we are using the "<br>" for putting newlines instead of the escape sequence character "
". This is because we show the output using innerHTML instead of a conventional string.
A string provided with a sentinel character or string for newline can be split into a corresponding array and then merged to create the required string embedded with newline characters.
Let us look at an example to see this use case.
We will create an array of strings from the provided single string and then merge them back to a single string using the join method providing the desired separator string.
Here we first split using the split() method and them join the array separated by newline characters using the join() method.
The join method can also be called for non-array objects. The method only expects a length property and integer accessible indices.
However, it has slightly different syntax from the conventional join.
var str = Array.prototype.join.call(arrayLikeObject, separator)
The first argument is the array like object and the second argument is the separator string. Again the second argument is optional, comma (,) being the default value.
Here’s an example −
Here we are going to create an array like objects with length property and integer indexed elements. Let’s see the working of join operator on the concerned object.
The join method first of all reads the length of the object. It then iterates on that many elements and joins them using the provided separator string.
Note however that if the length provided is shorter that the number of elements in the object, then only that many elements are joined. for example, in the code above, if we provided length as 3, then only first 3 elements would have been joined.
The join operator is pretty useful for merging arrays or array like objects into a single string. It is a generic method.
- How to insert a single line break in HTML?
- How to add a line break in an android textView?
- How to add a line break in an Android TextView using Kotlin?
- How to add line break for UILabel in iOS/iPhone?
- How to use break and continue statements in Java?
- How to use PowerShell break statement in foreach loop?