How can i console.log items from index 1 to 5 in current array? Using Loop
let cars = ["AUDI","BMW","LEXUS","VOLKSWAGEN","FERRARY","PORSCHE"]
const mappedCars=cars.map((item, i) => {
console.log("The current index is: " + i);
console.log("The current element is: " + item);
console.log("\n");
return item; //equivalent to list[index]
});
console.log(mappedCars);
cars.slice(1,6)console.log(). You should always be thinking of boundary conditions for your code. Please edit your question to describe exactly what you're trying to do and what should happen in unfavourable conditionsfor(var i=1; i<=Math.min(5, cars.length-1); i++) console.log("Index="+i+" Value="+cars[i]);If there are less than two cars, no output. if there are less than 6 cars, output will be less than 5 cars.