Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

why brackets instead of dot here?

const express = require('express');
const router = express.Router();
const data = require('../data/flashcardData.json').data
const cards = data.cards




router.get('/:id', (req, res)=>{
    const cardSide = req.query.cardSide
    const id = req.params.id
    const text = cards[id][cardSide]
    const hint = cards[id].hint

    const templateData = {text, hint}

    res.render('cards', templateData)
})

module.exports= router

const text = cards[id][cardSide] why do we use brackets on cardSide? why wouldn't it be a dot like with hint, since we are searching for the question/answer? Using a query to access a array index is new to me, thanks in advance!

1 Answer

Steven Parker
Steven Parker
243,215 Points

It might look like an array index, but its another way of accessing a property. The dot notation is good when you know the property name in advance. But when the property name is contained in a variable, the bracket notation is the only way to access that property.

Ok that makes sense to me, thanks!