In this article, we will look into some basic array questions in javascript that can be asked in your interview.
How do you check if an element exists in an array?
const array = [1, 2, 3, 4, 5];
const element = 3;
const exists = array.includes(element);
console.log(exists); // Output: true
In this example, we used the includes()
method to find the element in the array.
The includes()
method is used to determine whether a particular element is present in the array. It returns a Boolean value (true
or false
) based on whether the element exists in the array.
In our example, exists will be true because element 3 exists in the array.
How do you sort an array in ascending order?
const array = [5, 3, 1, 4, 2];
array.sort((a, b) => a - b);
console.log(array); // Output: [1, 2, 3, 4, 5]
In this example, we declare a constant variable named array
and assign it an array with values [5, 3, 1, 4, 2]
. We’re using the sort()
method with a custom comparison function provided as an argument.
The sort()
method is a built-in JavaScript method for arrays that sorts the elements of an array in place.
The comparison function (a, b) => a - b
sorts the array in ascending order by subtracting b
from a
. If the result is negative, a
is placed before b
. If it’s positive, b
is placed before a
.
After sorting the array in ascending order using the sort()
method with the comparison function, we get the sorted array [1, 2, 3, 4, 5]
.
How do you remove duplicates from an array?
const array = [1, 2, 3, 3, 4, 5, 5];
const uniqueArray = [...new Set(array)];
console.log(uniqueArray); // Output: [1, 2, 3, 4, 5]
In the example, the given array contains repeated values, including duplicates like 3
and 5
. We use the new Set(array)
to create a new Set from the elements of the array
. The [... ]
syntax is used to convert the Set back into an array.
A Set
is a built-in JavaScript data structure that stores unique values. It automatically removes duplicate values.
The output will be an array containing only the unique values from the original, which are [1, 2, 3, 4, 5]
How do you find the index of an element in an array?
const array = [10, 20, 30, 40, 50];
const element = 30;
const index = array.indexOf(element);
console.log(index); // Output: 2
In this example, we use indexOf()
to find the index of the element
(which is 30
) within the array
.
The indexOf()
method is a built-in JavaScript method for arrays that returns the first index at which a given element is found in the array. If the element is not found, the method returns -1
.
In this case, the value 30
is at index 2
within the array so the output is 2
.
How do you remove an element from an array by its index?
const array = [10, 20, 30, 40, 50];
const index = 2;
array.splice(index, 1);
console.log(array); // Output: [10, 20, 40, 50]
In this example, we use the splice() method to remove 1
element at the index specified by the value of the index
variable (which is 2
).
The splice()
method is a built-in JavaScript method for arrays that allows you to change the contents of an array by removing or replacing existing elements. The syntax is array.splice(startIndex, deleteCount)
.
After applying the splice()
method to the array, the element at the index 2
(which is 30
) is removed. In this case, the element 30
at index 2
is removed, resulting in the array [10, 20, 40, 50]
.
How do you concatenate two arrays?
const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const concatenatedArray = [...array1, ...array2];
console.log(concatenatedArray); // Output: [1, 2, 3, 4, 5, 6]
In this example, we declared two arrays array1 with elements [1, 2, 3] and array2 with elements [4, 5, 6]. Here, we use the spread syntax to concatenate array1
and array2
into a new array called concatenatedArray
.
The spread syntax ([...array]
) is used to expand the elements of an array into individual elements.
The resulting concatenatedArray
will contain all the elements from array1
followed by all the elements from array2
.
How to find the second-largest element present in the array?
In this example, we will use the sort function to sort the array and then return the second index.
var arr = ['20','120','111','215','54','78'];
arr.sort(function(a,b){
return b-a;
});
console.log(arr[1]);
The sort()
function takes a comparison function as its argument, which determines the order in which the elements are sorted. In this case, the comparison function subtracts a
from b
(b - a
), which means it sorts the elements in descending order.
The array will be sorted in descending order now. We will get the second-largest element using arr[1]