Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

255
Views
why can't console.log print with backward order index [-1]

i am new with JavaScript. Pls help.

I was playing with the method - console.log. here is what i did:

let iceCreamFlavors = ["Chocolate", "Strawberry", "Vanilla", "Pistachio", "Neapolitan", "Mint Chip"];
delete iceCreamFlavors[iceCreamFlavors.length-1];
console.log(iceCreamFlavors[length-1])

the console came back with

undefined

if I do:

console.log(iceCreamFlavors[5])

it has no problem to print

Mint Chip

but if I do:

console.log(iceCreamFlavors[-1])

it came back with

undefined

so my question is why can't console.log work with index numbers in the backward order? Maybe there is not much use in reality, but I am just curious.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

delete keyword is used to delete Object properties and not array elements, to remove an array element use Array.prototype.filter() or Array.prototype.splice()

Splice will modify the original array while filter will return a new array which passes the condition specified in callback.

let iceCreamFlavors = ["Chocolate", "Strawberry", "Vanilla", "Pistachio", "Neapolitan", "Mint Chip"];
iceCreamFlavors.splice(5, 1);
console.log(iceCreamFlavors);

let iceCreamFlavors = ["Chocolate", "Strawberry", "Vanilla", "Pistachio", "Neapolitan", "Mint Chip"];
const filter = iceCreamFlavors.filter(x => x != 'Mint Chip');
console.log(filter);

Note: you can access array elements using array[index], the index has a range from 0 to array.length - 1. Arrays starts from 0 index, which means first element will have index 0 and second will have index 1 so on

let iceCreamFlavors = ["Chocolate", "Strawberry", "Vanilla", "Pistachio", "Neapolitan", "Mint Chip"];
iceCreamFlavors.splice(5, 1);
console.log(iceCreamFlavors.length); // array length
console.log(iceCreamFlavors[iceCreamFlavors.length - 1]); // last element
console.log(iceCreamFlavors[0]); // first element

delete vs splice in array

delete will not change the length or reindex the array, the given element is removed but it is appeared as undefined

splice will completely remove the element along with changing the length and reindex the elements

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!