6) Advanced Arrays, Strings and Functions Lesson

Strings vs Arrays in JavaScript

7 min to complete · By Ian Currie

Think about the concept of a string. At its core, a string is essentially a sequence or collection of individual characters arranged consecutively. When you think about this arrangement, it's quite akin to an array – a fundamental data structure in programming that holds a series of elements in a specific order.

Arrays in JavaScript are versatile structures that can hold various data types, including numbers, strings, or even other arrays. They're indexed collections, meaning each element has a specific position, known as an index, starting from zero.

Now, consider a string in JavaScript. When you break it down, a string can be thought of as an array-like structure where each character occupies a position, much like elements in an array. This similarity raises a question: Can you interact with strings using methods and approaches typically reserved for arrays? To a large extent, the answer is yes!

Indexing in Strings and Arrays

You can use square bracket notation on strings to access individual characters, similar to how you would access elements in an array. For example, consider the following code:

let evenNumbers = [2, 4, 6, 8, 10];
let numberString = "2,4,6,8,10";

console.log(evenNumbers[0]); // Outputs 2
console.log(evenNumbers[1]); // Outputs 4

console.log(numberString[0]); // Outputs "2"
console.log(numberString[1]); // Outputs ","

In this example, both the array evenNumbers and the string numberString are accessed using index positions. Notice how the numberString is treated much like an array, with each character (including commas) having its own unique index.

Moreover, the length property in JavaScript offers insights into the size of the array or string:

let evenNumbers = [2, 4, 6, 8, 10];
let numberString = "2,4,6,8,10";

console.log(evenNumbers.length); // 5
console.log(numberString.length); // 10

For the array evenNumbers, length returns 5, representing the number of elements. However, for the string numberString, length returns 10, as it counts each character, including commas and spaces, as individual elements.

This array-like nature of strings in JavaScript, as it is in most languages, can be very handy. Consider a for loop, a fundamental control structure used for iterating over elements. You can use a for loop to traverse a string, character by character, similar to how you would iterate through an array:

let numberString = "2,4,6,8,10";

for (let i = 0; i < numberString.length; i++) {
  console.log(numberString[i]);
}

/* OUTPUT
2
,
4
,
6
,
8
,
1
0
*/

In this loop, each iteration accesses a single character of the string using its index, printing out each character in turn.

While strings share some similarities with arrays, they are distinct types with their own set of properties and methods. You'll examine a few of those differences in the next lesson.

Differences Between Arrays and Strings in JavaScript

While strings and arrays in JavaScript share some similar characteristics, such as being indexed collections of elements, they are inherently different in several crucial ways:

Aspect Arrays Strings
Element Type Can contain multiple data types. Consists of characters only.
Mutability Mutable: elements can be added, removed, or changed. Immutable: once created, cannot be altered.
Methods Offers methods for dynamic manipulation (e.g., .push(), .pop()). Methods return new strings and do not modify the original (e.g., .replace(), .slice()).
Use Cases Suitable for dynamic collections and complex data structures. Ideal for fixed textual data.
Iteration Elements can be iterated and modified. Can be iterated over, but not modified.
Type-Specific Methods Includes array-specific methods like .isArray(), .sort(). String-specific methods focus on text manipulation, like .indexOf(), .trim().

In summary, while arrays and strings in JavaScript may appear similar at first glance due to their indexed nature, they differ significantly in terms of their mutability, the nature of their elements, and the methods available for their manipulation. Arrays offer more dynamic and versatile options for handling multiple data types and are mutable, while strings are best for dealing with fixed sequences of characters and are immutable.

Summary: Strings vs Arrays in JavaScript

You've seen the parallels and distinctions between strings and arrays in JavaScript.

  • Indexed Access: Both strings and arrays allow element access using index positions.
  • Data Types: Arrays handle multiple data types, whereas strings consist of sequential characters.
  • Mutability: Arrays are mutable, but strings are immutable.
  • Method Availability: Arrays offer dynamic manipulation methods, while string methods create new strings.