JavaScript

Split a String and get the First Array Element in JavaScript

In the process of sorting the data in bulk or in the case of separating the entries based on various attributes, there can be misspelled words. In such case scenarios, splitting a string and getting the first array element in JavaScript is very effective in the proper formatting, improved readability of data, and managing the data in a readable manner.

This blog will explain the approaches to split a string and get the first element from an array in JavaScript.

How to Split the Specified String and Get the First Array Element in JavaScript?

A string can be split and the first array element in JavaScript can be fetched by using the following approaches in combination with the “split()” method:

  • Indexing” technique.
  • shift()” method.
  • slice()” method

Approach 1: Split a String and Get the First Array Element in JavaScript Using Indexing Technique

The “split()” method splits a particular string into a substrings array. This method can be applied in combination with indexing to split the provided string into multiple strings in an array and then fetch the first string value by indexing it.

Syntax

string.split(separator, limit)

In the above syntax:

  • separator” refers to the string that needs to be used for splitting.
  • limit” points to the integer that limits the number of splits.

Example

Let’s overview the following example:

<script type="text/javascript">
let string = 'Linux,hint,Website';
console.log("The given string is:", string)
let split = string.split(',');
console.log("The array of the given string becomes:", split)
console.log("The first array element is:", split[0])
</script>

According to the above code snippet:

  • Specify the stated string value and display it.
  • After that, apply the “split()” method such that the given string is split into multiple string values and then contained in an array.
  • Lastly, retrieve the first array element by specifying its index and display it.

Output

From the above output, it can be observed that the given string is split into an array, and the first array element is fetched.

Approach 2: Split a Particular String and Get the First Array Element in JavaScript Using shift() Method

The “shift()” method removes or deletes the first array item and changes the original array as well. This method can be utilized in combination with the “split()” method to split the given string into an array of strings and access the first array element directly.

Example

The below-given example explains the discussed concept:

<script type="text/javascript">
let string = "Java,Script"
console.log("The given string is:", string)
let first = string.split(",");
console.log("The array of the given string becomes:", first)
console.log("The first array element is:", first.shift());
</script>

Go through the following steps:

  • Firstly, specify a string value and display it.
  • Likewise, apply the “split()” method and display the array of the split string values.
  • Finally, invoke the “shift()” method to fetch the first array element directly.

Output

Approach 3: Split a String and Get the First Element From an Array in JavaScript Using slice() Method

The “slice()” method accesses the selected array elements in the form of a new array without changing the original array. This method can be applied in combination with the “split()” method to similarly split the specified string into an array of strings and access the first array element with respect to the passed indexes.

Syntax

array.slice(start, end)

In the given syntax:

  • start” and “end” correspond to the start and end positions, respectively.

Example

Let’s follow the below-stated example:

<script type="text/javascript">
let string = "Split,a,string"
console.log("The given string is:", string)
let first = string.split(",");
console.log("The array of the given string becomes:", first)
console.log("The first array element is:", first.slice(0,1));
</script>

In the above lines of code:

  • Recall the discussed approaches for specifying a string and splitting it.
  • After that, apply the “slice()” method with the stated parameters referring to the index of the first array element.
  • This will resultantly access to the first array element from the split string.

Output

From the above output, it is evident that the first array element is retrieved.

Conclusion

The indexing technique can be implemented to split the string into an array of strings and point to the first array element. The combined shift() method can be utilized to similarly split the string and directly retrieve the first array element. The slice() method, in combination, can be applied to perform the desired requirement by pointing to the index of the first array element. This tutorial explained how to split a specific string and get the first array element in JavaScript.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.