JavaScript - TypedArray join() Method
The JavaScript TypedArray join() method is used to create and return a string by concatenating typed array elements and separating them with a comma (the default separator) or the specified separator string. If the current typed array contains only one element, the element will be returned as is without using the separator.
If the current typed array is empty (or arr.length is 0), an empty string is returned.
Syntax
Following is the syntax of JavaScript TypedArray join() method −
join(separator)
Parameters
This method accepts a parameter named 'separator', which is described below −
separator (optional) − The string separates each element of the typed array while concatenating.
Return value
This method returns a string concatenated with all typed array elements.
Examples
Example 1
If the separator parameter is omitted, it concatenates the typed array elements, separated by commas.
In the following program, we are using the JavaScript TypedArray join() method to concatenates this typed array elements [1, 2, 3, 4, 5, 6, 7, 8] separated with commas.
<html>
<head>
<title>JavaScript TypedArray join() Method</title>
</head>
<body>
<script>
const T_array = new Uint16Array([1, 2, 3, 4, 5, 6, 7, 8]);
document.write("Typed array: ", T_array);
//using join() method
let new_str = "";
new_str = T_array.join();
document.write("<br>New concatenated string: ", new_str);
</script>
</body>
</html>
Output
The above returns the concatenated string with comma separated as −
Typed array: 1,2,3,4,5,6,7,8 New concatenated string: 1,2,3,4,5,6,7,8
Example 2
If we pass the separator parameter value as "−", it will concatenate the typed array elements and separate them with the specified separator.
The following is another example of the JavaScript TypedArray join() method. We use this method to concatenate this typed array elements [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] and separate them with the specified separator string "−".
<html>
<head>
<title>JavaScript TypedArray join() Method</title>
</head>
<body>
<script>
const T_array = new Uint16Array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100]);
document.write("Typed array: ", T_array);
const separator_str = "-";
document.write("<br>Separator: ", separator_str);
//using join() method
let new_str = "";
new_str = T_array.join(separator_str);
document.write("<br>New concatenated string: ", new_str);
</script>
</body>
</html>
Output
After executing the above program, it will return a new concatenated string separated by "−".
Typed array: 10,20,30,40,50,60,70,80,90,100 Separator: - New concatenated string: 10-20-30-40-50-60-70-80-90-100
Example 3
If the current typed array has only one element "new Uint16Array([100])", then the same element will be returned without using a separator.
<html>
<head>
<title>JavaScript TypedArray join() Method</title>
</head>
<body>
<script>
const T_array = new Uint16Array([100]);
document.write("Typed array: ", T_array);
const separator_str = "-#-";
document.write("<br>Separator: ", separator_str);
//using join() method
let new_str = "";
new_str = T_array.join(separator_str);
document.write("<br>New concatenated string: ", new_str);
</script>
</body>
</html>
Output
Once the above program is executed, it will return the concatenated string without using a separator.
Typed array: 100 Separator: -#- New concatenated string: 100