Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

259
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda