[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/Optional_chaining [Back]  [Original]

(?.) - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

(?.)

Baseline

20207

(?.) (optional chaining) undefined null undefined

const adventurer = {
  name: "Alice",
  cat: {
    name: "Dinah",
  },
};

const dogName = adventurer.dog?.name;
console.log(dogName);
// : undefined

console.log(adventurer.someNonExistentMethod?.());
// : undefined

js
obj?.prop
obj?.[expr]
func?.(args)

?. . null undefined undefined undefined

obj

js
const nestedProp = obj.first && obj.first.second;

obj.first null undefined obj.first.second obj.first obj.first.second

JavaScript obj.first null undefined 0nestedProp 0

(?.) obj.first.second obj.first

js
const nestedProp = obj.first?.second;

?. . JavaScript obj.first.second obj.first null undefined obj.first null undefined undefined

js
const temp = obj.first;
const nestedProp =
  temp === null || temp === undefined ? undefined : temp.second;

undefined

js
undeclaredVar?.prop; // ReferenceError: undeclaredVar is not defined

API

undefined

js
const result = someInterface.customMethod?.();

?. TypeError "someInterface.customMethod is not a function"

: ?. TypeError ("someInterface is null") someInterface null undefined ?. someInterface?.customMethod?.()

eval?.() eval

js
const propName = "x";
const nestedProp = obj?.[propName];

js
function printMagicIndex(arr) {
  console.log(arr?.[42]);
}

printMagicIndex([0, 1, 2, 3, 4, 5]); // undefined
printMagicIndex(); // undefined; if not using ?., this would throw an error: "Cannot read properties of undefined (reading '42')"

js
const object = {};
object?.property = 1; // SyntaxError: Invalid left-hand side in assignment

SyntaxError: tagged template cannot be used with optional chain

js
String?.raw`Hello, world!`;
String.raw?.`Hello, world!`; // SyntaxError: Invalid tagged template on optional chain

new SyntaxError: new keyword cannot be used with an optional chain

js
new Intl?.DateTimeFormat(); // SyntaxError: Invalid optional chain from new expression
new Map?.();

null undefined

js
const potentiallyNullObj = null;
let x = 0;
const prop = potentiallyNullObj?.[x++];

console.log(x); // x  0

js
const potentiallyNullObj = null;
const prop = potentiallyNullObj?.a.b;
// 

js
const potentiallyNullObj = null;
const prop =
  potentiallyNullObj === null || potentiallyNullObj === undefined
    ? undefined
    : potentiallyNullObj.a.b;

js
const potentiallyNullObj = null;
const prop = (potentiallyNullObj?.a).b;
// TypeError: Cannot read properties of undefined (reading 'b')

js
const potentiallyNullObj = null;
const temp = potentiallyNullObj?.a;
const prop = temp.b;

temp

CSS name undefined

js
const myMap = new Map();
myMap.set("JS", { name: "Josh", desc: "I maintain things" });

const nameBar = myMap.get("CSS")?.name;

?.

js
// 
function doSomething(onContent, onError) {
  try {
    // 
  } catch (err) {
    // onError 
    if (onError) {
      onError(err.message);
    }
  }
}
js
// 
function doSomething(onContent, onError) {
  try {
    // 
  } catch (err) {
    onError?.(err.message); // onError 
  }
}

js
const customer = {
  name: "Carl",
  details: {
    age: 82,
    location: "Paradise Falls", // 
  },
};
const customerCity = customer.details?.address?.city;

// 
const customerName = customer.name?.getName?.(); //  customerName  undefined

js
function printCustomerCity(customer) {
  const customerCity = customer?.city ?? "Unknown city";
  console.log(customerCity);
}

printCustomerCity({
  name: "Nathan",
  city: "Paris",
}); // "Paris"
printCustomerCity({
  name: "Carl",
  details: { age: 82 },
}); // "Unknown city"

ECMAScript 2027 LanguageSpecification
# prod-OptionalExpression


Web Proxy Viewer  |  New URL  |  Original Page