0

let will_pokemon = {
    pikachu: {
        species: 'Mouse pokemon',
        height: 0.4,
        weight: 5
    }
}

let samson_pokemon = {
    raichu = {
        species: 'Rare pokemon',
        height: 0.8,
        weight: 12
    }
}

let weight4 = samson_pokemon?.pikachu?.weight //check either object property available if not will be undefined

console.log(weight4)

Why I got error Invalid shorthand property initializer when running on my Chrome browser?

frost21
  • 3
  • 2

2 Answers2

1

You do not use '=' inside object literals. It should be raichu : {

let samson_pokemon = {
    raichu : {
        species: 'Rare pokemon',
        height: 0.8,
        weight: 12
    }
}
Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39
0

you can do this.

let weight4 = (typeof samson_pokemon !== 'undefined') ? samson_pokemon.pikachu : samson_pokemon.weight;
cbrr09
  • 189
  • 8