Get the First Word of a String in JavaScript

avatar

Borislav Hadzhiev

3 min

banner

Photo from Unsplash

Get the First Word of a String in JavaScript #

To get the first word of a string in JavaScript:

  1. Use the String.split() method to split the string into an array of words.
  2. Access the array at index 0 to get the first word of the string.
index.js
const str = 'Hello world'; const first = str.split(' ')[0] console.log(first); // ๐Ÿ‘‰๏ธ Hello

The String.split() method splits a string into an array based on the provided separator.

index.js
const str = 'Hello world'; const split = str.split(' ') console.log(split) // ๐Ÿ‘‰๏ธ ['Hello', 'world']

We split the string on each space to get an array of the words in the string.

The last step is to access the array element (word) at index 0.

JavaScript indexes are zero-based, so the index of the first element in the array is 0 and the index of the last element is arr.length - 1.

You can also use the Array.at() method to get the first array element.

index.js
const str = 'Hello world'; const first = str.split(' ').at(0); console.log(first); // ๐Ÿ‘‰๏ธ Hello

The Array.at() method takes an integer value and returns the item at that index.

If the index is not contained in the array, the method returns undefined.

If your string starts with a space, use the String.trim() method to remove any leading and trailing whitespace characters before calling the String.split() method.

index.js
const str = ' Hello world'; const first = str.trim().split(' ')[0]; console.log(first); // ๐Ÿ‘‰๏ธ Hello
We used the String.trim() method to remove the leading and trailing whitespace from the string and then called the String.split() method.

This is necessary because if the string starts with a whitespace character, the first element in the array would be an empty string.

index.js
const str = ' Hello world'; // ๐Ÿ‘‡๏ธ [ '', 'Hello', 'world' ] console.log(str.split(' '));

Alternatively, we can use the Array.shift() method.

Get the First Word of a String using Array.shift() #

To get the first word of a string in JavaScript:

  1. Use the String.split() method to split the string into an array of words.
  2. Use the Array.shift() method to remove and return the first element from the array.
index.js
const str = 'Hello world'; const first = str.split(' ').shift(); console.log(first); // ๐Ÿ‘‰๏ธ Hello

We used the String.split() method to split the string into an array of words.

index.js
const str = 'Hello world'; // ๐Ÿ‘‡๏ธ [ 'Hello', 'world' ] console.log(str.split(' '));

The Array.shift method removes and returns the first element from the array.

Alternatively, you can use the String.slice() method.

Get the First Word of a String using String.slice() #

To get the first word of a string in JavaScript:

  1. Use the String.indexOf() method to get the index of the first space in the string.
  2. Use the String.slice() method to get the part of the string before the first space.
index.js
const str = 'Hello world'; const firstWord = str.slice(0, str.indexOf(' ')); console.dir(firstWord); // ๐Ÿ‘‰๏ธ 'Hello'

We used the String.indexOf() method to get the index of the first occurrence of a space in the string.

index.js
const str = 'Hello world'; console.log(str.indexOf(' ')); // ๐Ÿ‘‰๏ธ 5

We passed the following arguments to the String.slice() method:

  1. start index - the index of the first character to be included in the new string.
  2. end index - extract characters up to, but not including this index.

The String.slice() method returns the first word of the string, or in other words, the part of the string before the first space.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2023 Borislav Hadzhiev