Nullish coalescing operator
function printMessage(text) {
let message = text;
if (text == null || text == undefinde) {
message = "Noting to display;
}
console.log(message);
}
function print message(text) {
const message = text ?? "noting to display"
console.log(message)
```;
}
기본 파라미터일때는 null 인 경우는 안됨
Logicla or operator ||
leftExpr || rightExpr
leftEXpr 가 거짓일때 rightExpr 실행
(false, 0, nan, -0, "", undefinde, null)
function print message(text) {
const message = text || "noting to display"
console.log(message);
}
const result = getInitialState() ?? fetchFromServer();
console.log(result);
function getInitialState() {
return null;
}
function fetchFromServer() {
return "Hiya from ';
}
function displayJobTitle(person) {
if (person.job && person.job.title) {
console.log(person.job.title);
}
}
=>
function displayJobTitle(person) {
if (person.job?.title) {
console.log(person.job.tutle);
}
}
function displayJobTitle(person) {
const title = personjob?.title ?? "No Job Yet";
console.log(title)
}