All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

JavaScript Split String Into Array

Last Updated : Mar 11, 2024

JavaScript Split String Into Array

In this tutorial we will show you the solution of JavaScript split string into array, here we used split() method for split string into an array. The split() method splits a string into an array of substrings.

The split() method returns the new array. The split() method does not change the original string. If (’ ’) is used as separator, the string is split between words.

Step By Step Guide On JavaScript Split String Into Array :-

Here we defined input tag for getting string input from user dynamically and submit button defined for submit user input value then defined two div tags ‘h,res’ for displaying heading, result of split array on webpage.

In script we collecting user input by Id ‘str’ and stored value on variable ‘inpt’ then we appends with split() method for splits the string into separated characters and result stored on variable ‘arr’.

Finally we appended result and defined heading on webpage by innerHTML.

<!DOCTYPE html>
        <title>SPLIT STRING INTO ARRAY</title>
        <input id="str" type="text" placeholder="Enter a String">
        <input onclick="fun()" type="submit" value="Submit">
        <div id="h"></div>
        <div id="res"></div>
            function fun(){
                var inpt=document.getElementById('str').value;
                var arr=inpt.split("");
                document.getElementById('h').innerHTML="SPLITED STRING RESULT<br>";
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. Here we defined two input tags for getting input string from user and submit button for submit user string on script when user clicks on submit button by function ‘fun()’.
  7. Two div tags defined with id attributes ‘h, res’. In script we defined function fun(), here user input collected by ‘str’ and stored on variable ‘inpt’.
  8. Using that variable we appended split() method with double quotes so it will separates each characters by comma and stored on variable ‘arr’.
  9. We appended heading ‘SPLITED STRING RESULT’ on div element ‘h’ and appended result of array on div element ‘res’ so it will displayed on webpage.
  10. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to split a string into array of characters using javascript.

When we executes program on browser we can see the input box with submit button.

User needs to give input string and clicks on submit button then user gave string will separated by comma displayed on webpage with program concept heading.

If user not filled input box and clicks on submit button then we can’t see the result so user have to confirm whether input box filled or not.

I hope this tutorial on JavaScript split string into array helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪