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:

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:

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.

Understanding Strings

Think of strings as a sequence of letters or characters. So, whether you're writing down a message or noting a paragraph, it all boils down to a string in JavaScript. Strings are enclosed by double quotes or single quotes.

Though strings are immutable, we can use string methods such as .toLowerCase(), .toUpperCase(), and .trim() to effectively work with them. These methods essentially create a new string for us.

Indexing and Common Operations

Both arrays and strings allow us to access individual elements through indexing. In JavaScript, we start counting from 0, implying the first element is at index 0, the second at index 1, and so on. Negative indexing is not directly supported in JavaScript, but you can work around it by adjusting indices based on the length of the collection.

We have many operations we can perform on our lists and strings. We can slice them, concatenate them, and even find an occurrence of a particular element!

When using the spread operator for concatenation (...myList), the elements of myList are expanded into individual elements and combined with additional elements [6, 7, 8].

Template literals, enclosed by backticks (`), allow you to embed expressions inside a string using ${expression}. This provides a cleaner and more readable way to concatenate strings compared to using the + operator.

The spread operator, used in [...myList], creates a shallow copy of myList, ensuring the original array remains unchanged. The sort method with a comparison function (a, b) => b - a sorts the array in non-increasing order. Here's how it works:

  • The comparison function receives two arguments, a and b.
  • If the returned value is negative, a should come before b.
  • If the returned value is positive, b should come before a.
  • If the returned value is zero, their positions are unchanged relative to each other.

By subtracting a from b, it sorts the elements in descending order (from highest to lowest). Thus, sortedList will contain [5, 4, 3, 2, 1], a sorted copy of myList in descending order.

Lesson Summary and Practice

Give yourself a pat on the back! You've navigated through arrays and strings, learning how to create, access, and manipulate them via various operations.

Up next, reinforce your understanding with plenty of hands-on practice. The comprehension of these concepts, combined with frequent practice, will enable you to tackle more complex problem statements with ease. Happy coding!

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