JavaScript String Split

Split method is one of the most important methods of string object. Sometimes it happened that you want to convert a string into an array based on some separator character so for the same you can use split method of string object in JavaScript. Split method requires one string argument which is a separator then split method divide the string in small chunk through the separator string and write in an array element. Every time split method found the separator string it creates a new array element. The example of split method mentioned below: Code


<html>
  <head>
    <title>JavaScript String Split</title>
  </head>
  
  <body>
    <script language='JavaScript' type='text/Javascript'>
      var weekDaysString = 'Sunday,Monday,Tuesday,Wednesday,Thursday,
      Friday,Saturday';
      weekDaysArray = weekDaysString.split(',');
    </script>
  </body>
</html>
In above mentioned example we have a string object with the name of weekDaysString which have all the week days name in comma separated format. In the next line we have used split function with weekDaysString string object and provide separator string as comma(,) then split function returns an array of all the week days and we store it in weekDaysArray.