JavaScript

String Array in JavaScript

Arrays are one of the most significant and commonly used data structures in programming. In JavaScripts arrays can be of various types such as numeric, strings, etc. if we talk about the string array, it is nothing but an array of strings. As the name itself suggests the string array can store the fixed number of string values only. The string arrays are very similar to the numbers array.

This write-up will explain the below-listed aspects of the String Array:

  • How to Use Traditional String Arrays
  • How to Use String Array as an Object
  • How to Use Built-in Methods with String Array

So, let’s get started!

How to Use Traditional String Arrays

As the name itself indicates it is a normal array just like numeric arrays, boolean arrays. The array indexing will start from 0. The below snippet will show you how to declare an array in JavaScript:

var arrayValues = ["Java", "JavaScript", "Python", "C++", "PHP"];

Here, “var” is a keyword used to declare any variable, “arrayValues” is a user-defined array name, while “Java”, “JavaScript”, etc. are the elements of the array.

Example
In this example, firstly, we will declare and initialize a string array and afterward we will use the for-loop to print each array element on the browser’s console:

<script type="text/javascript">
  var arrayValues = ["Java", "JavaScript", "Python", "C++", "PHP"];
  console.log("Array Values: ");
   for (let i = 0; i <= arrayValues.length-1; i++) {
    console.log(arrayValues[i]);
   }
</script>

The above snippet will generate the below-given output:

In this way, we can work with string arrays in JavaScript.

How to Use String Array as an Object

If we talk about a string array as an object, it utilizes the key-value pair.

Example
In this example, we will learn how to use string array as an object in JavaScript:

<script type="text/javascript">
  var arrayValues = {1: "Java", 2: "JavaScript", third: "PHP", fourth: "Python" };
  console.log(arrayValues[1]);
  console.log(arrayValues["third"]);
  console.log(arrayValues["fourth"]);
</script>

In this example, firstly, we created a string array as an object, afterward, we accessed different elements the string array and printed them on the browser’s console:

Output shows that the above program is working properly.

How to Use Built-in Methods with String Array

In JavaScript, multiple built-in methods such as concat(), includes(), split(), etc. can be used with string arrays to achieve different functionalities.

Example
This example will explain the working of split() method in JavaScript:

<script type="text/javascript">
  var message = "Welcome to the linuxhint.com!";
  var splitValues = message.split(" ");
  console.log(splitValues[3]);  
</script>

In this example, we utilized the split() method and passed it a “white space” as a parameter. Consequently, it will split the string whenever a white space will be encountered in that string. Finally, we printed the value of the third index:

Output verifies the working of split() method.

Example
This example will explain the working of concat() method in JavaScript:

<script type="text/javascript">
  var arrayValues1 = ["Java", "JavaScript"];
  var arrayValues2 = ["Python", "C++", "PHP"];
  var concatValues= arrayValues1.concat(arrayValues2);
  console.log(concatValues);  
</script>

In this example, initially, we created two arrays, next, we utilized the concat() method to concatenate the values of both arrays:

The output displayed the concatenated array.

Example
This example will explain the working of includes() method in JavaScript:

<script type="text/javascript">
  var arrayValues1 = ["Java", "JavaScript"];
  var result= arrayValues1.includes("PHP");
  console.log(result);  
  var result= arrayValues1.includes("Java");
  console.log(result);
</script>

In this example we utilized the includes() method to check the existence of the “PHP”, and “JAVA” in the string array:

The output verifies the working of include() methods.

Similarly, there are many more methods that can be used with the string arrays to achieve different functionalities.

Conclusion

The string arrays can store the fixed number of string values only. In JavaScript, string arrays can be used as either traditional string arrays or as an Object. Traditional string arrays are normal arrays just like numeric arrays, boolean arrays, etc. While the string array as an object utilizes the key-value pair. JavaScript provides multiple built-in methods such as concat(), includes(), split(), etc. that can be used with the string arrays to achieve different functionalities. This post explained different aspects of string arrays in with the help of suitable examples.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.