1

I need to write in my object some properties with the ' character e.g.

const championsList = {
    Kha'Zi: '...',
};

How can I do that?

j08691
  • 204,283
  • 31
  • 260
  • 272
gs fs
  • 31
  • 3
  • While Yannick's answer will work, I would question whether you really *need* to use special characters for this. Your code will be a lot cleaner if you can avoid it. – John Montgomery Jun 03 '20 at 21:34
  • I'm getting these data from a site for a bot i need to write it in this way – gs fs Jun 03 '20 at 21:35

1 Answers1

0

You can do so by wrapping the property in quotation marks, having a property like this does mean you'll have to access it using brackets:

const championsList = {
  "Kha'Zi": "...",
};
console.log(championsList[ "Kha'Zi" ]);
Yannick K
  • 4,887
  • 3
  • 11
  • 21
  • 2
    You don't need the brackets, just the quotes. Brackets are only needed if the property name is a dynamic expression. – Barmar Jun 03 '20 at 21:34
  • @Barmar yup, I'm an idiot. Thanks for the comment, edited. – Yannick K Jun 03 '20 at 21:37
  • if i set a variable: let scelta = "Kha'Zix"; const championsList = { "kha'Zi": '....', }; let sceltaUrl = championsList[scelta]; console.log(sceltaUrl); it returns undefined – gs fs Jun 03 '20 at 21:41
  • @gsfs note that you didn't capitalize "Kha'Zi" in your object there. And actually, `scelta` is equal to `Kha'Zix` not `Kha'Zi`. – Yannick K Jun 03 '20 at 21:45