Arrays in JavaScript
Arrays in JavaScript
Arrays in JavaScript
Topics Covered
What is an Array
Why do we need arrays in JavaScript
Declaration of Array
Array index and storing
Accessing elements in an arra
Changing values in an arra
Delete keyword in arrays & its proble
Iterating of an Array ite
Interview Point
What is an Array?
An array in JavaScript is a data structure that stores an
ordered list of elements. It can hold elements of any data type,
including numbers, strings, objects, and even other arrays.
Arrays are a type of object in JavaScript and have a number of
built-in methods for adding, removing, and manipulating
elements.
Array length property - it is used to get the length of the array, ie array.length
Note - it starts counting from one.
Example -
Example of an array:
Grouping related data: Arrays allow you to group related data together and make it easier to manipulate.
For example, you could use an array to store a list of names or a list of products.
Storing large amounts of data: Arrays can store large amounts of data in a single variable, making it easier
to manage and manipulate the data. This can be especially useful when working with large datasets
Improving performance: Arrays can improve performance when working with large amounts of data.
Because the elements in an array are stored in consecutive memory locations, it is faster to access
elements in an array than it is to access elements in other data structures, such as objects
Ease of use: Arrays come with a number of built-in methods for adding, removing, and manipulating
elements. This makes it easy to perform operations on the data stored in an array
Better readability: Arrays make your code more readable by grouping related data together in a single
structure. This makes it easier for others to understand your code and makes it easier for you to maintain
your code in the future.
Overall, arrays in JavaScript are a crucial tool for managing and manipulating data in various locations. They
provide a flexible and efficient way to store and process data and make writing clean, maintainable code
easier.
Declaration of Array:
Using square brackets [ ]: This is the most common and recommended way to declare an array in
JavaScript. You can create an array by enclosing a comma-separated list of elements in square brackets:
It is generally not recommended to create an array using the Array constructor in JavaScript, due to the
following reasons -
Inconsistent behaviour - The behaviour of the Array constructor is inconsistent depending on the number
and type of arguments passed to it. If you pass a single numeric argument, it is treated as the desired
length of the array, and an empty array with that length is created. If you pass multiple arguments, those
arguments become the elements of the array. This can lead to confusion and unexpected results
Simplicity and readability - Using an array literal notation ([]) to create an array is simpler and more
readable. It is a widely adopted convention in JavaScript code and provides a clear and concise way to
define arrays
Potential security risks - Using the Array constructor with a single numeric argument can lead to potential
security risks if the value is not properly validated. If an attacker is able to supply a malicious value as the
argument, it could result in unexpected behavior or vulnerabilities.
Using an array literal: This is a shorthand method for creating arrays that are equivalent to using square
brackets:
In JavaScript, arrays are zero-indexed, meaning that the first element has an index of 0, the second element
has an index of 1, and so on. To store a value in an array, you can use the square bracket notation and the index
number:
You can access any element in the array using its index number:
It's important to keep in mind that if you try to access an index that is outside the bounds of the array, you will
get undefined.
And also if you try to access a negative index it will give you an undefined.
In some other programming languages like Java, C, and C++ if you try to access a negative index, you will get
an error which is ArrayIndexOutOfBoundException or Invalid OutOfRange.
Overall, accessing elements in an array is a basic operation in JavaScript that allows you to retrieve values
stored in the array. Understanding how to access elements in an array is essential for working with arrays in
JavaScript.
Example:
The above example is used to change the value of an array using the index of the particular array items.
Sparse Arrays - The delete keyword does not update the length property of the array. Therefore, if you
delete an element using delete, the length of the array remains the same, giving a false impression of the
array's actual size
Array length - The delete keyword does not update the length property of the array. Therefore, if you delete
an element using delete, the length of the array remains the same, giving a false impression of the array's
actual size
Performance Impact - Deleting an element with delete does not reclaim the memory associated with that
element. It simply marks the element as undefine
Array Integrity - Using delete on an array can disrupt the integrity of the array's order and indices. The
deleted element is replaced with an empty slot, and subsequent elements are not automatically shifted to
fill the gap
for… of loop
It is a newer JavaScript feature introduced in ECMAScript 6 (ES6) that allows us to iterate over iterable arrays,
including objects.
Example -
while loop
The while loop allows us to loop over an array by checking a condition before each iteration.
Example -
Ans. The delete keyword in JavaScript arrays is used to remove an element at a specific index. However, it
doesn't actually remove the element; instead, it leaves an undefined gap at the specified index. A potential
issue is that it doesn't update the array length, and iterating over the array may encounter unexpected
undefined values.
Q. Explain the role of the “const” keyword when working with arrays in JavaScript. How does using const
impact common array operations like adding or removing elements? Explain with an example.
Ans. In JavaScript, the const keyword is used to declare constants, indicating that the assigned value cannot
be reassigned. However, when const is applied to arrays, it doesn't make the array itself immutable. The
reference to the array is constant, meaning we cannot reassign the variable to a new array, but the array
elements can be modified.
Example:
But when we try to use the same variable name for another array, then javascript will throw us an error
Example:
output:
Ans. The delete keyword in JavaScript arrays is used to remove an element at a specific index. However, it
doesn't actually remove the element; instead, it leaves an undefined gap at the specified index. A potential
issue is that it doesn't update the array length, and iterating over the array may encounter unexpected
undefined values.
Q. Write a JavaScript function that takes an array of numbers as an argument and returns the largest
element in the array
Ans.