*A recent addition
As it treats null and undefined similarly, we'll use a special term here, in this article. We'll say that an expression is "defined" when it's neither null nor undefined.
null도 undefined도 아니면 defined이라고 표현하기로 한다.
The result of a ?? b is:
In other words, ?? returns the first argument if it's not null/undefined. Otherwise, the second one.
다시말해서 ?? 는 null/undefined이 아니라면 첫 번째 인수를 리턴한다. 그렇지 않으면 두번째 인수를 리턴한다.
let user;
alert(user ?? "Anonymous"); // Anonymous
첫 번째 인수인 user가 undefined이기 때문에 두번째 인수인 Anonymous가 리턴된다.
We can also use a sequence of ?? to select the first value from a list that isn't null/undefined.
The important difference between them is that:
다시 말해서, || 는 false
, 0
, an empty string ""
and null/undefined
의 차이를 구분할 수 없다. 그들은 모두 falsy value이다.
The precedence of the ?? operator is the same as ||.
operator ?? is evaluated before = and ?, but after most other operations, such as +, *.
operator ?? 를 다른 오퍼레이터와 같이 쓸때, parantheses 괄호를 같이 쓰는 것을 고려해야 한다.
괄호를 사용해서 확실히 구분해주지 않는 이상, ?? 와 &&, || 를 같이 쓰는 것을 금지한다.