Baseline Widely available
ë ë³í© ì°ì°ì (??
) ë ì¼ìª½ í¼ì°ì°ìê° null ëë undefinedì¼ ë ì¤ë¥¸ìª½ í¼ì°ì°ì를 ë°ííê³ , ê·¸ë ì§ ìì¼ë©´ ì¼ìª½ í¼ì°ì°ì를 ë°ííë ë
¼ë¦¬ ì°ì°ìì´ë¤.
ì´ë ì¼ìª½ í¼ì°ì°ìê° null
ëë undefined
ë¿ë§ ìëë¼ falsy ê°ì í´ë¹í ê²½ì° ì¤ë¥¸ìª½ í¼ì°ì°ì를 ë°ííë ë
¼ë¦¬ ì°ì°ì OR (||
)ìë ëì¡°ëë¤. ë¤ì ë§í´ ë§ì½ ì´ë¤ ë³ì fooìê² falsy ê°( ''
ëë 0
)ì í¬í¨í ê°ì ì ê³µí기 ìí´ ||
ì ì¬ì©íë ê²ì ê³ ë ¤íë¤ë©´ ìê¸°ì¹ ìë ëìì´ ë°ìí ì ìë¤. íë¨ì ë ë§ì ìì ê° ìë¤.
ë ë³í© ì°ì°ìë ì°ì°ì ì°ì ììê° ë¤ì¯ë²ì§¸ë¡ ë®ìë°, ||
ì ë°ë¡ ìëì´ë©° ì¡°ê±´ë¶ (ì¼í) ì°ì°ìì ë°ë¡ ìì´ë¤.
const foo = null ?? "default string";
console.log(foo);
// Expected output: "default string"
const baz = 0 ?? 42;
console.log(baz);
// Expected output: 0
ë¬¸ë² ì¤ëª
ë ë³í© ì°ì°ìë ë§ì½ ì¼ìª½ ííìì´ null
ëë undefined
ì¸ ê²½ì°, ì¤ë¥¸ìª½ ííìì 결과를 ë°ííë¤.
ì´ì ìë ë³ìì 기본ê°ì í ë¹íê³ ì¶ì ë, ë
¼ë¦¬ ì°ì°ì OR (||
)ì ì¬ì©íë ê²ì´ ì¼ë°ì ì¸ í¨í´ì´ë¤:
let foo;
...
// foo is never assigned any value so it is still undefined
let someDummyText = foo || 'Hello!';
ê·¸ë¬ë ||
boolean ë
¼ë¦¬ ì°ì°ì ë문ì, ì¼ìª½ í¼ì°ì°ìë booleanì¼ë¡ ê°ì ë¡ ë³íëìê³ falsy í ê°(0
, ''
, NaN
, null
, undefined
)ì ë°íëì§ ììë¤. ì´ ëìì ë§ì½ 0
, ''
or NaN
ì ì í¨í ê°ì¼ë¡ ìê°í ê²½ì° ìê¸°ì¹ ìë 결과를 ì´ëí ì ìë¤.
let count;
let text;
...
count = 0;
text = "";
...
let qty = count || 42;
let message = text || "hi!";
console.log(qty); // 42 and not 0
console.log(message); // "hi!" and not ""
ë ë³í© ì°ì°ìë 첫 ë²ì§¸ ì°ì°ìê° null
ëë undefined
ë¡ íê°ë ëë§, ë ë²ì§¸ í¼ì°ì°ì를 ë°íí¨ì¼ë¡ì¨ ì´ë¬í ìíì í¼íë¤:
let myText = ""; // An empty string (which is also a falsy value)
let notFalsyText = myText || "Hello world";
console.log(notFalsyText); // Hello world
let preservingFalsy = myText ?? "Hi neighborhood";
console.log(preservingFalsy); // '' (as myText is neither undefined nor null)
ë¨ë½
ORê³¼ AND ê°ì ë
¼ë¦¬ ì°ì°ìë¤ê³¼ ë§ì°¬ê°ì§ë¡, ë§ì½ ì¼ìª½ì´ null
ëë undefined
ê° ìëì´ íëª
ëë©´ ì¤ë¥¸ìª½ ííìì íê°ëì§ ìëë¤.
function A() {
console.log("A was called");
return undefined;
}
function B() {
console.log("B was called");
return false;
}
function C() {
console.log("C was called");
return "foo";
}
console.log(A() ?? C());
// logs "A was called" then "C was called" and then "foo"
// as A() returned undefined so both expressions are evaluated
console.log(B() ?? C());
// logs "B was called" then "false"
// as B() returned false (and not null or undefined), the right
// hand side expression was not evaluated
No chaining with AND or OR operators
AND (&&
) ì OR ì°ì°ì (||
)를 ??
ì ì§ì ì ì¼ë¡ ê²°í©íì¬ ì¬ì©íë ê²ì ë¶ê°ë¥íë¤. ì´ ê²½ì° SyntaxError
ê° ë°ìëë¤.
null || undefined ?? "foo"; // raises a SyntaxError
true || undefined ?? "foo"; // raises a SyntaxError
ê·¸ë¬ë ì°ì ìì를 ëª ìì ì¼ë¡ ëíë´ê¸° ìí´ ê´í¸ë¥¼ ì¬ì©íë©´ ê°ë¥íë¤:
(null || undefined) ?? "foo"; // returns "foo"
Optional chaining ì°ì°ì(?.
)ìì ê´ê³
ë ë³í© ì°ì°ìë ëª
íí ê°ì¼ë¡ undefined
ê³¼ null
ì ì²ë¦¬íê³ , optional chaining ì°ì°ì (?.
)ë null
or undefined
ì¼ ì ìë ê°ì²´ì ìì±ì ì ê·¼í ë ì ì©íë¤.
let foo = { someFooProp: "hi" };
console.log(foo.someFooProp?.toUpperCase()); // "HI"
console.log(foo.someBarProp?.toUpperCase()); // undefined
ìì
ì´ ìì ë 기본 ê°ì ì ê³µíì§ë§ null
or undefined
ì´ì¸ì ê°ì 를 ì ì§íë¤.
function getMiscObj() {
return {
aNullProperty: null,
emptyText: "", // this is not falsy
someNumber: 42,
};
}
const miscObj = getMiscObj();
const newObj = {};
newObj.propA = miscObj.aNullProperty ?? "default for A";
newObj.propB = miscObj.emptyText ?? "default for B";
newObj.propC = miscObj.someNumber ?? 0;
console.log(newObj.propA); // "default for A"
console.log(newObj.propB); // "" (as the empty string is not null or undefined)
console.log(newObj.propC); // 42
ëª
ì¸ì ë¸ë¼ì°ì í¸íì± ì°¸ê³
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4