The split() is a built-in JavaScript String method used to split an input string into an array of strings by separating it into substrings using a separator provided in the argument.
Syntax
string.split(separator, limit)
Parameters
Arguments | Value |
separator | The separator argument is used to specify the character or the regular expression for splitting the string. |
limit |
The limit argument is the upper limit on the number of splits in the given string. |
Return value
It returns an array of strings formed at each position where the separator occurs.
Steps
- Split the CSV string into rows using the newline character as a delimiter.
- Iterate through each row and split it into individual values using the comma character as a delimiter.
- Add the resulting arrays of values to the final array.
Example
function csvToArray(csv) {
const rows = csv.split('\n');
const result = [];
for (const row of rows) {
const values = row.split(',');
result.push(values);
}
return result;
}
const csvString = 'Name,Age,Occupation\nKeval Shah,30,Developer\nEsha Shah,25,Designer';
const array = csvToArray(csvString);
console.log(array);
Output
[
[ 'Name', 'Age', 'Occupation' ],
[ 'Keval Shah', '30', 'Developer' ],
[ 'Esha Shah', '25', 'Designer' ]
]
If you need to parse more complex CSV data, consider using a dedicated CSV parsing library like Papa Parse that can handle these cases correctly.

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.