Kevin Powell - CommunityKP-C
Kevin Powell - Community•3y ago•
25 replies
ƅ Marlon G

JS in JSX for newbie

Hello!

What is the square brackets in the const img doing here:
import React from 'react';
import { createRoot } from 'react-dom/client';

const container = document.getElementById('app');
const root = createRoot(container);
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']}
/>;

root.render(img); 


Are they allowing for the ternary function to choose the pics property and connecting them to coinToss() in an array?
Was this page helpful?