Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Convert Comma Separated String to Array in JavaScript</title> </head> <body> <script> var names = 'Harry,John,Clark,Peter,Rohn,Alice'; var nameArr = names.split(','); console.log(nameArr); // Accessing individual values document.write(nameArr[0] + "<br>"); // Prints: Harry document.write(nameArr[1] + "<br>"); // Prints: John document.write(nameArr[nameArr.length - 1] + "<br>"); // Prints: Alice var str = 'Hello World!'; var chars = str.split(''); console.log(); // Accessing individual values document.write(chars[0] + "<br>"); // Prints: H document.write(chars[1] + "<br>"); // Prints: e document.write(chars[chars.length - 1]); // Prints: ! </script> </body> </html>