0

I have the following simple literal object:

const ob = { 'xxx:': 'abc', aaaa: 3, ccc: [1, 2, 3] };

let's say i'm not sure that i have a ccc key in the object so i want to use Optional Chaining but i'm getting a syntax error when trying to run:

const ddd = ob.ccc?.[0];

What is the correct syntax that takes the value only if *ccc key exist?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Eitanos30
  • 1,331
  • 11
  • 19
  • 1
    Where are you trying to run this? This is the correct syntax, it works just fine for me. Likely your environment doesn't support optional chaining yet, and you need to update it or use a transpiler. – Bergi Dec 26 '20 at 21:44
  • 1
    That is the correct syntax. Probably you are using a JS engine that isn't new enough to support optional chaining. – Quentin Dec 26 '20 at 21:44
  • @Bergi, microsoft edge :( – Eitanos30 Dec 26 '20 at 21:44
  • https://caniuse.com/?search=optional%20chaining — You need to be using Edge 80 or newer. – Quentin Dec 26 '20 at 21:45
  • Can you please tell me what is my version :) I found only this : Microsoft Edge 44.18362.449.0 and i didn't find 44 versoin at the link you just sent – Eitanos30 Dec 26 '20 at 21:49
  • https://en.wikipedia.org/wiki/Microsoft_Edge — you have a version from May 21, 2019. You need Feb 2020 or newer. – Quentin Dec 26 '20 at 21:52

1 Answers1

1

As Quentin said, the Optional chaining operator doesn't support the legacy version Microsoft Edge(Edge Html 12~18, the Microsoft Edge 44.18362.449.0 using Microsoft EdgeHTML 18.18363). More detail information, you could check the following links:

The optional chaining operator Browser compatibility

JavaScript operator: Optional chaining operator

To use the Optional chaining operator, you have to use the new Microsoft Edge, and upgrade the Microsoft Edge browser Version to 80+ version. You can download the latest version Microsoft Edge from here.

Reference: EdgeHtml

Zhi Lv
  • 18,845
  • 1
  • 19
  • 30