Array and Utility Functions in JavaScript
Explore essential JavaScript array functions including creating arrays, accessing elements, and using methods like push, pop, shift, unshift, and indexOf. This lesson helps you understand how to manipulate arrays as stacks and queues, build utility logic, and solve coding challenges involving array elements for interactive web projects.
Introduction
Normally, an array is the name given to a data structure that contains data elements of a similar type. However, in JavaScript, an array can contain elements of different types. All these elements are stored in a contiguous memory location. For example, below are two valid arrays in JavaScript:
// Different types of elements in an array
array_one = ["Car", "Bus", "Truck", 18.0, 90]
// Same type of elements in an array
array_two = ["Car", "Bus", "Truck"]
Let’s start exploring the different methods we can use on arrays in JavaScript.
Basic operations on arrays
In this section, we will explore some basic operations that can be performed on arrays, such as creating an array, printing the elements of an array, accessing an element from an array, and so on.
Explanation
-
Line 1: We define an array of vehicles and name it
vehicles...