Open In App

Lodash _.join() Method

Last Updated : 02 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Lodash _.join() function is used to convert all elements in the array into a string separated by a separator.

Syntax:  

_.join(array, [separator=',']);

Parameter:

  • array: It is the original array from which the join operation is to be performed.
  • separator: A string to separate each element of the array. If leave it by default array element is separated by comma(, ).

Return Value:

  • This function returns a string created by joining all the elements of the array using the separator.

Example 1: In this example, the _.join() method joins together the elements of the array into a string using ‘|’.

JavaScript
// Requiring the lodash library 
let _ = require("lodash");

// Original array to be joined 
let array = [1, 2, 3, 4, 5, 6];

let newArray = _.join(array, '|');
console.log("Before Join: " + array);

// Printing newArray  
console.log("After Join: " + newArray); 

Output:

Example 2: In this example, the _.join() method joins together the elements of the array into a string using ‘, ‘ since it is the default value.

JavaScript
// Requiring the lodash library 
let _ = require("lodash");

// Original array to be joined 
let array = [1, 2, 3, 4, 5, 6];

let newArray = _.join(array);
console.log("Before Join: " + array);

// Printing newArray  
console.log("After Join: " + newArray); 

Output:

Example 3: In this example, the _.join() method joins together the elements of the array into a string using ‘ ‘ (empty string).

JavaScript
// Requiring the lodash library 
let _ = require("lodash");

// Original array to be joined 
let array = [1, 2, 3, 4, 5, 6];

let newArray = _.join(array, '');
console.log("Before Join: " + array);

// Printing newArray  
console.log("After Join: " + newArray); 

Output:


Similar Reads