Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

258
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda