Skip to content

split method in JavaScript | Code

  • by

JavaScript split() method is used to splits a String object into an array of strings by separating the string into substrings. This method takes a pattern and divides a String into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.

string.split(separator, limit)

Note: This method does not change the original string and If (” “) is used as a separator, the string is split between words.

The split method in JavaScript

A simple example code divides a string into a list of substrings and returns them as an array.

<!DOCTYPE html>
  <script >
    const str = 'Hello World!';

    const words = str.split(' ');

    const chars = str.split('');

    const strCopy = str.split();


split method in JavaScript | Code

Use the limit parameter:

const myArray = text.split(" ", 3);// How,are,you

Split a string into characters and return the second character:

let text = "How are you doing today?";
const chars = text.split("");

console.log(chars[1]); // 0

Do comment if you have any doubts or suggestions on this Js string split topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *