Skip to main content
X

Explore your training options in 10 minutes

JavaScript Split: A Step-By-Step Guide

James Gallagher - January 04, 2021


The JavaScript split() method divides a string into an array of substrings. These substrings are added to a new array. split() returns the new array of substrings.


When you’re programming, it’s common to see cases where you need to get some text from a larger string. Say you want to get a user’s first and last name from one string separately. How would you go about getting that information from a string?

That’s where the JavaScript split() method comes in. split() divides a string into a list of substrings. In this guide, we’re going to discuss how to use the split() method, with reference to examples.

Get offers and scholarships from top coding schools illustration

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses










By continuing you agree to our Terms of Service and Privacy Policy , and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

String Refresher

Before we start looking at the split() and slice() functions, we should remind ourselves how strings work. Strings are a sequence of one or more characters that may include letters, symbols, or numbers.

Each character in a string can be accessed using its index number, starting with 0. Let’s use an example to illustrate how a string is indexed:

H e l l o
t h e r e
0 1 2 3 4 5 6 7 8 9 10

As you can see, the first character in the string is “H,” which has an index value of “0”. The last character is “e,” which has an index value of “10.” Note that the space between the two words also has an index value.

JavaScript Split

The JavaScript string split() method splits up a string into multiple substrings. These substrings are stored in a new array. The original string is divided based on a specified separator character, like a space.

Let’s look at the syntax for the JavaScript string split() method:

var new_list = text.split(character, limit);

The split() method is added to the end of our “text” variable. Our text variable is split based on the separator character we specify. We assign the result of the split() method to the JavaScript variable new_list .

The limit parameter specifies how many JavaScript substrings you want to include in your final array.

You must assign the output of the split() method to a variable. This is because split() creates a new list. It does not modify your existing string.

JavaScript Split String Example

Say that a user has given us their first and last names. We store this information in one string. To get the user’s first and last names separately, we could use the split() method.

For example, when a user signs in to their account, we may want to say “Hello, [first name]” instead of displaying their full name.

Venus, a software engineer at Rockbot

"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"

Venus, Software Engineer at Rockbot

We’re going to use a white space character (which is the space character) to separate out our string:

var fullName = “Forename Surname”;
var fullNameSplit = fullName.split(“ “);

console.log(fullNameSplit);

This code returns the following:

[“Forename”, “Surname”]

The split() function has split our string into an array of substrings. Now, we can access each string in the JavaScript array object using an index number. For example:

fullNameSplit[1];

This code returns:

Surname

The split() method returns an array from which we can retrieve individual values. Each array element corresponds with a word in our string. We retrieved the value with the index position 1 from our array.



Split JavaScript String into One Array

You can use the split() method to move the contents of a string into an array. Consider the following code:

var name_as_list = name.split();
console.log(name_as_list);

Our code returns: “Forename Surname”. We did not specify a separator character which means that multiple substrings are not created. We now have an element consisting of the entire string stored in the variable name_as_list.

Conclusion

The split() method divides a string into a list of substrings. It is used to make individual words or values in a string accessible by indexing. split() can separate out a string by any character, such as a white space or a comma.

If you’re looking to learn more about JavaScript, read our guide on the best tutorials for JavaScript beginners .

About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.

What's Next?

James Gallagher

About the author: James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.

Skip to main content