Skip to content

WebDevHubs

  • Home
  • HTML
  • CSS
  • JavaScript
  • Web Technologies
  • Web Templates
  • Toggle search form

JavaScript Array join() Method

Posted on December 31, 2024December 31, 2024 By Admin No Comments on JavaScript Array join() Method

The Array.join() method is a built-in JavaScript function used to join all elements of an array into a single string. The elements are separated by a specified delimiter or a comma (,) if no delimiter is provided.

Syntax

arr.join(separator);

Parameters

ParameterDescription
separator(Optional) A string is used to separate array elements in the resulting string. Defaults to a comma (,) if not provided.

Return Value

The join() method returns a string containing all array elements joined by the specified separator. If the array is empty, it returns an empty string.

Example 1: Joining Array Elements with Default Separator

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = ['HTML', 'CSS', 'JavaScript'];
let result = arr.join();
console.log(result);
// Output: 'HTML,CSS,JavaScript'
let arr = ['HTML', 'CSS', 'JavaScript']; let result = arr.join(); console.log(result); // Output: 'HTML,CSS,JavaScript'
let arr = ['HTML', 'CSS', 'JavaScript'];
let result = arr.join();
console.log(result); 
// Output: 'HTML,CSS,JavaScript'

The default separator (a comma) is used to join the array elements.

Example 2: Joining with a Custom Separator

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = ['HTML', 'CSS', 'JavaScript'];
let result = arr.join(' - ');
console.log(result); // Output: 'HTML - CSS - JavaScript'
let arr = ['HTML', 'CSS', 'JavaScript']; let result = arr.join(' - '); console.log(result); // Output: 'HTML - CSS - JavaScript'
let arr = ['HTML', 'CSS', 'JavaScript'];
let result = arr.join(' - ');
console.log(result); // Output: 'HTML - CSS - JavaScript'

Example 3: Joining an Array of Numbers

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [10, 20, 30, 40, 50];
let result = arr.join(' | ');
console.log(result);
// Output: '10 | 20 | 30 | 40 | 50'
let arr = [10, 20, 30, 40, 50]; let result = arr.join(' | '); console.log(result); // Output: '10 | 20 | 30 | 40 | 50'
let arr = [10, 20, 30, 40, 50];
let result = arr.join(' | ');
console.log(result); 
// Output: '10 | 20 | 30 | 40 | 50'

Example 4: Joining an Empty Array

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [];
let result = arr.join();
console.log(result); // Output: ''
let arr = []; let result = arr.join(); console.log(result); // Output: ''
let arr = [];

let result = arr.join();
console.log(result); // Output: ''

Example 5: Handling undefined and null Values

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [1, undefined, null, 4];
let result = arr.join(' - ');
console.log(result);
// Output: '1 - - - 4'
let arr = [1, undefined, null, 4]; let result = arr.join(' - '); console.log(result); // Output: '1 - - - 4'
let arr = [1, undefined, null, 4];

let result = arr.join(' - ');
console.log(result); 
// Output: '1 -  -  - 4'

Example 6: Joining with No Separator

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = ['A', 'B', 'C'];
let result = arr.join('');
console.log(result); // Output: 'ABC'
let arr = ['A', 'B', 'C']; let result = arr.join(''); console.log(result); // Output: 'ABC'
let arr = ['A', 'B', 'C'];

let result = arr.join('');
console.log(result); // Output: 'ABC'

Example 7: Joining Nested Arrays

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [1, [2, 3], 4];
let result = arr.join();
console.log(result); // Output: '1,2,3,4'
let arr = [1, [2, 3], 4]; let result = arr.join(); console.log(result); // Output: '1,2,3,4'
let arr = [1, [2, 3], 4];

let result = arr.join();
console.log(result); // Output: '1,2,3,4'

Supported Browsers

BrowserSupport
Chrome1+
Firefox1+
Safari1+
Edge12+
Opera4+
Internet Explorer5.5+
JavaScript, Web Technologies Tags:JavaScript-Array, JavaScript-Array-Method, JavaScript-Method

Post navigation

Previous Post: JavaScript Array.from() Method
Next Post: Best Way to Find if an Item Is in a JavaScript Array

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Archives

  • June 2025
  • May 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024

Categories

  • CSS
  • HTML
  • JavaScript
  • Lodash
  • PHP
  • Python
  • Web Technologies
  • Web Templates

Recent Posts

  • JavaScript Array isArray() Method
  • JavaScript Array forEach() Method
  • JavaScript Array includes() Method
  • JavaScript Array keys() Method
  • JavaScript Array lastIndexOf() Method

Recent Comments

No comments to show.

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme