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

271
Vistas
What is an array right after a variable called in JavaScript?

I'm curious about this line and have never seen this format with an array right after the variable:

pics[coinToss() === 'heads' ? 'kitty' : 'doggy'] to render pics.doggy or pics.kitty

Is there specific documentation about this format I can read more about?

import ReactDOM from 'react-dom';

function coinToss () {
  // Randomly return either 'heads' or 'tails'.
  return Math.random() < 0.5 ? 'heads' : 'tails';
}

const pics = {
  kitty: 'https://content.codecademy.com/courses/React/react_photo-kitty.jpg',
  doggy: 'https://content.codecademy.com/courses/React/react_photo-puppy.jpeg'
};

const img = <img src={pics[coinToss() === 'heads' ? 'kitty' : 'doggy']} />;

ReactDOM.render(
    img, 
    document.getElementById('app')
);
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It's not an array, it's a property access - like when you access some array's elements with myArray[5] to get the 6th element (which is property 5 of the array), but it works the same for string properties of objects: pics['kitty'] is the same as pics.kitty.

The name of the property accessed is whatever coinToss() == 'heads' ? 'kitty' : 'doggy' evaluates to (which uses ?: to yield either 'kitty' or 'doggy' depending on the result of coinToss()).

Here it's functionally the same as coinToss() === 'heads' ? pics.kitty : pics.doggy.

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