Array — data type in JavaScript:

In JavaScript, objects are like containers where we store data in pairs, like keys and values. They’re super useful and used all the time. But sometimes, we need to keep our data in a specific order, like a list of numbers [1, 2, 3, 4, 5, 6], where each number has a position starting from 0 and going up. Objects can’t do this easily.

That’s where arrays come in. Arrays are another way to store data, but they keep it in a specific order. You can think of them like numbered boxes where you can put things in a row. This makes them handy for when you need to keep things organized in a sequence.

const vehicals = ["cycle", "bike", "cars", "trucks"]; // string array
 
const marks = [99, 98, 95, 92]; // numeric array

const contactNumber = [1234567890, '+91-1234567890']; // combined = numeric + string array

Array has static parameter i.e. length which gives the length of an array. This gets updated each time when user updates an Array.

const avengers = ["captain-america", "thor", "iron-man"];
const totalAvengers = avengers.length // 3

Array methods in JavaScript

push(element) — This array method allows us to add the new element at the end of an array.

const f1Manufacturer = ["Red bull", "Mercedes", "Meclaren", "Ferrari", "Haas"];

// push

console.log("Before push: ", f1Manufacturer); // "Before push: ", ["Red bull", "Mercedes", "Meclaren", "Ferrari", "Haas"]

f1Manufacturer.push("Aston Martin"); 

console.log("After push: ", f1Manufacturer); // "After push: ", ["Red bull", "Mercedes", "Meclaren", "Ferrari", "Haas", "Aston Martin"]

pop() — This array method allows us to remove the element from the end of an array.

const f1Manufacturer = [“Red bull”, “Mercedes”, “Meclaren”, “Ferrari”, “Haas”];

// pop

console.log(“Before pop: “, f1Manufacturer); // “Before pop: “, [“Red bull”, “Mercedes”, “Meclaren”, “Ferrari”, “Haas”]

f1Manufacturer.pop();

console.log(“After pop: “, f1Manufacturer); // “After pop: “, [“Red bull”, “Mercedes”, “Meclaren”, “Ferrari”]

 slice(start-position, end-position(optional)) — This array method allows us to take the part of an array. Here, the start-position is inclusive and the end position is exclusive. Also the end-position is optional parameter.

How to get the highest and lowest number from an array in JavaScript?

Now let’s use the Math.max() and Math.min() methods and use the spread operator on the array like this.