โoptional chaining (?., ?.[])
`obj ?. prop` syntax. ๐ง under construction -> SyntaxError: Unexpected token
JS โฉ statement โฉ expression โฉ operator โฉ left-hand side โฉ
(operator) (โญ๏ธ ES2020) (๐ chaining rules | table of operators )
obj?.prop returns undefined if obj is nullish.
obj ?. prop // dot notation
obj ?. [ expr ] // bracket notation
func ?. ( args ) // conditional invocation๐ฏ synonyms๏ผ"conditional property access", "optional chaining"
codepen๏ผoptional chaining
// โญ๏ธ 1. prop: invalid identifier
// ------------------------------
undeclared ?. 1 = 'bad'; // โ SyntaxError: Unexpected token
// ^
// โญ๏ธ 2. obj: "undeclared"
// -------------------------------
undeclared ?. prop; // โ ReferenceError: 'undeclared' is not defined
// โญ๏ธ 3. obj: "nullish"
// -------------------------------
null ?. prop // โ undefined
// โญ๏ธ 4. obj: not "nullish"
// -------------------------------
(18) ?. prop; // โ undefined (no such `prop`)
(18) ?. toString(16); // โ "12" (property value, could be any value)
// test object
let person = {
name : 'Tom',
age : 28,
address: { street: 'Fifth Ave.', no: '23' },
hobbies: ['TV', 'Game'],
eat(food) { return 'poop' }
};
'------------ person ------------',
// โ "root" object (person) is NEVER protected
person ?. name, // "Tom"
// โฐ๐ก๏ธโฏ
person ?. hobby, // undefined
// โฐ๐ก๏ธโโฏ
person ?. address ?. street, // "Fifth Ave."
// โฐโ๐ก๏ธโโโฏ โฐโ๐ก๏ธโโฏ
person . address ?. district, // undefined (โ
address: non-nullish)
// โฐโโโโโฏ โฐโโ๐ก๏ธโโโฏ
person . hobby ?. [1], // undefined (โ
hobby: nullish, protected)
// โฐโโโฏ โฐ๐ก๏ธโฏ
person . hobbies ?. [2], // undefined (โ
hobbies: non-nullish)
// โฐโโโโโฏ โฐ๐ก๏ธโฏ
person ?. eat ?. ('egg'), // "poop" (โ
eat: non-nullish)
// โฐ๐ก๏ธโฏ โฐโ๐ก๏ธโโโฏ
person ?. make ?. (something), // undefined (โ
make: direct prop, protected, short-circuiting)
// โฐ๐ก๏ธโโฏ โฐโโโ๐ก๏ธโโโโโฏ
// jane
let jane = {
home : null,
boyfriend: { name: 'Joe' }
};
'------------ jane ------------',
// โ "root" object (jane) is NEVER protected
jane . home, // null (prop value)
// โฐโโฏ
jane ?. home, // null (jane: non-nullish, = jane.home)
// โฐ๐ก๏ธโฏ
// ๐ก๏ธ: "?." protect "nullish" obj and "direct" prop
// โ: no protection, could be undeclared / nullish / error
// -----------------------------------------------------------
jane . home ?. c , // undefined (โ
home: null, protected)
// โฐโโฏ โฐ๐ก๏ธโฏ
jane . home ?. ['hello'], // undefined (โ
home: null, protected)
// โฐโโฏ โฐโโโ๐ก๏ธโโโโฏ
jane . home ?. c . d , // undefined (โ
home: null, short-circuiting)
// โฐโโฏ โฐ๐ก๏ธโฏ โฐโโฏ
// โญโ UD โโฎ // UD: UnDefined
// (a.b?.c).d, // โ DON'T DO THIS! (โ TypeErrorโ)
// ^
jane . boyfriend ?. name, // 'Joe' ( โ
boyfriend: non-nullish)
// โฐโโโโโโโฏ โฐ๐ก๏ธโฏ
jane . boyfriend ?. money, // undefine ( โ
money: direct prop, protected)
// โฐโโโโโโโฏ โฐ๐ก๏ธโโฏ
jane . boyfriend ?. money . more, // โ TypeError (accessing `undefined.more`)
// โฐโโโโโโโฏ โฐ๐ก๏ธโโฏ โฐโโโฏ // (โ more: not protected)
jane . boyfriend ?. money ?. more, // undefined โ
(more: direct prop, protected)
// โฐโโโโโโโฏ โฐ๐ก๏ธโโฏ โฐ๐ก๏ธโโฏ โญ๏ธ the variable before ?. must be declaredโ๏ธ
?. only protects nullish obj and direct propโ
โญ๏ธ use ?. for reading / deleting, but not writingโ๏ธ
the right bracket notation [] for optional chaining
while optional chaining, don't use (...) in the middle to prevent "short-circuiting" from happeningโ
โญ ?. protects nullish obj and direct prop, so if obj๏ผdeclared, prop๏ผidentifier, then it's totally safe to call obj ?. prop.
โญ ใไธ็ขบๅฎๅญไธๅญๅจใ็็ฉไปถๆๅฑฌๆง๏ผๆ้่ฆ็จ ?.
โญ use ?. followed by ?? to provide a default value.
โญ๏ธ ๆณจๆ๏ผ
็ถ็่ฆฝๅจๆ JavaScript engine ๆฒๆๆญคๅ่ฝๆ๏ผๅฏ็จ obj.prop(path) ๆไธ้ข็ๆนๅผ่งฃๆฑบ๏ผ
replit โฉ optional chaining ( support๏ผโ )
๐ ่งฃ่ฅ๏ผ
๐chaining rules
๐ฐshort-circuiting
๐
obj . prop/obj [ prop ](non-optional chaining).๐
condition ? a : bโ use
obj ?. prop??defaultto provide default value.๐ฉน use obj.prop(path) instead if
?.not supported.
replit๏ผoptional chaining (use cases)
How to use optional chaining in Node.js 12 (โญ supported from Node 14.0.0โ)
reddit โฉ JS Optional Chaining Polyfill?
SyntaxError: Unexpected token
Last updated
Was this helpful?