JavaScript Arrays and Strings for Interview Prep

Introduction

Welcome to this course!

Before we delve deeper into JavaScript essentials for interview prep, let's start with some foundational JavaScript features — specifically, arrays and strings. These features allow JavaScript to group multiple elements, such as numbers or characters, under a single entity.

Revising Arrays and Strings

As our starting point, it's crucial to understand how arrays and strings function in JavaScript. An array is a built-in object that provides a way to store multiple values in a single variable and is mutable (we can change it after creation), while strings are immutable (unalterable post-creation). Let's see examples:

const myList = [1, 2, 3, 4];
let myString = "hello";

// Now let's try to change the first element of both these features
// Using index to change the element at the specified index (0 in this case)
myList[0] = 100;
myString[0] = "t"; // does not change anything
// The following method does not change the string in place,
// but returns a new string where 'h' is replaced with 'H'
myString.replace('h', 'H');
// So it is possible to obtain a new string like this:
const newString = myString.replace('h', 'H');

console.log(myList); // prints [100, 2, 3, 4]
console.log(myString); // prints hello
console.log(newString); // prints Hello

Diving Into Lists

Arrays in JavaScript allow us to organize data so that each item holds a definite position or an index. The index allows us to access or modify each item individually. For accessing elements, JavaScript arrays use zero-based indexing. This means the first element in the array is accessed with index 0, the second element with index 1, and so on. To get the last element of the array, we subtract one from the array's length (fruits.length - 1 in the example below).

Working with arrays in JavaScript is as simple as this:

const fruits = ["apple", "banana", "cherry"];

// Add a new element at the end
fruits.push("date"); // ['apple', 'banana', 'cherry', 'date']

// Inserting an element at a specific position
fruits.splice(1, 0, "bilberry"); // ['apple', 'bilberry', 'banana', 'cherry', 'date']

// Removing a particular element
fruits.splice(fruits.indexOf("banana"), 1); // ['apple', 'bilberry', 'cherry', 'date']

// Accessing elements using indexing
const firstFruit = fruits[0]; // apple
const lastFruit = fruits[fruits.length - 1]; // date

The splice method takes three parameters:

  1. The start index where elements will be added or removed.
  2. The number of elements to remove from the start index.
  3. The elements to add at the start index.

In the example fruits.splice(fruits.indexOf("banana"), 1);:

  • fruits.indexOf("banana") returns the index of the element "banana", which is 2 in this case.
  • The first parameter 2 specifies the start index for removal.
  • The second parameter 1 indicates that we want to remove one element starting from index 2.

As a result, the element "banana" at index 2 is removed from the array.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal