How to use the split() function In Javascript

Converting CSVs into Arrays: Using the split() function Now let us see how we can convert a string with comma-separated values into an Array. 

javascript




let csv = "geeks, 4, geeks"
let array = csv.split(", ");
console.log(array[0]);
console.log(array[1]);
console.log(array[2]);


Output

geeks
4
geeks



Thus split() method of String comes in handy for this. It is used to split a string into an array of substrings and returns the new array. The split() method does not change the original string.



Converting JavaScript Arrays into CSVs and Vice-Versa

Converting Arrays into CSVs: Given an array in JavaScript and the task is to obtain the CSVs or the Comma Separated Values from it. Now, JavaScript being a versatile language provides multiple ways to achieve the task.

Some of them are listed below.

Similar Reads

Method 1: Using the toString() method

In this method, we will use the toString() method o obtain the CSVs or the Comma Separated Values....

Method 2: Using valueof() Method

...

Method 3: Using the join() function

The valueOf() method returns the primitive value of an array. Again the returned string will separate the elements in the array with commas. There is no difference between toString() and valueOf(). Even if we try with different data types like numbers, strings, etc it would give the same result....

Method 4: Using the split() function

...