JavaScript and comma separated string



We can create a single variable that contains string data, which this string data holds several data separated by comma.

For example, I create this string: “Andy,New York,28,Journalist”. This single string contains 4 data separated by comma. Now the problem is how to extract this 4 data from it?

The answer is using JavaScript “split” function. Here is the sample code:

By using split() function I’ve created an array variable, that each one of it indexes contains separated string data respectively. In above example, data1 which is separatedData[0] contains “Andy” and data2 contains “New York” and so on.



Published by

Habibie

Habibie is an Indonesian freelance frontend-backend web developer and cross platform mobile app programmer. He's also good at graphics design, video editing, visual effects and animation.