The split()
method in JavaScript is used to split a string into an array of substrings, based on a specified separator string. This method takes a single argument, which is the separator string to use when splitting the string. The split()
method splits the original string at each occurrence of the separator string, and returns an array of substrings.
Here is an example of how the split()
method works:
const string1 = 'Hello, world!';
const result = string1.split(', ');
// the result will be: ['Hello', 'world!']
In the code example above, we first declare a string called string1
that contains the text Hello, world!
. We then use the split()
method to split the string1
string at each occurrence of the separator string ', '
. The split()
method takes a single argument, which is the separator string to use when splitting the string. In this case, we pass in the string ', '
as the argument to the split()
method.
As a result of calling the split()
method, the result
variable will be set to ['Hello', 'world!']
, which is an array of substrings containing the two substrings that were separated by the ', '
separator string in the string1
string. If the string1
string did not contain the ', '
separator string, the result
variable would be set to an array containing a single element, which is the original string1
string.
Leave a Reply