![]() It doesn't make any modifications to the original string. Here is a string created using string literals: let message = 'I am a Happy Go lucky Guy' Let's understand how this works with an example. We can call the split() method on the message string. Let's split the string based on the space ( ' ') character. Split using a space characterĬonsole.log(arr) // Here the space character acts as a splitter or divider. Access each of the elements of the array. The main purpose of the split() method is to get the chunks you're interested in from a string to use them in any further use cases. You can split a string by each character using an empty string('') as the splitter. In the example below, we split the same message using an empty string. ![]() ![]() The result of the split will be an array containing all the characters in the message string. ![]() You may get this as a question in your upcoming job interviews! ''.split('') // returns How to Split a String into One Array console.log(message.split('')) // □ Please note that splitting an empty string('') using an empty string as the splitter returns an empty array. ![]() You can invoke the split() method on a string without a splitter/divider. This just means the split() method doesn't have any arguments passed to it. When you invoke the split() method on a string without a splitter, it returns an array containing the entire string. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |