??
.result = a ?? b
// result = (a !== null && a !== undefined) ? a : b;
alert(firstName ?? lastName ?? nickName ?? "Anonymous"); // Supercoder
// alert(firstName || lastName || nickName || "Anonymous");
||
height || 100
checks height for being a falsy value, and it’s 0, falsy indeed.??
together with &&
and ||
operators, unless the precedence is explicitly specified with parentheses.The “do…while” loop
- do {} while
The “for” loop
- for (begin; condition; step) {}
- Skipping parts
for(i=0; i<3; i++)
for(; i<3; i++)
for(; i<3;)
for(;;)
break
let sum = 0;
while () {
let value = +prompt("Enter a number", '');
if (!value) break;
sum += value;
}
alert('Sum' + sum);
+prompt("Enter a number", '');
for(i=0; i<10; i++) {
if(i % 2 == 0) continue
alert(i);
}
control flow statement
continue
can’t use in?
ternary operator. conditional expression of?
must be expression
labels for break/continue
- labelName: for(...) {...}
outer: for(let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
let input = prompt(`Value at coords(${i}, ${j})`, '');
if(!input) break outer;
}
}
alert("Done!")
2.5 Data Types - String_ link
3 types of quotes - Backticks
Backticks are “extended functionality” quotes. They allow us to embed variables and expressions into a string by wrapping them in ${…}, for example:
while
do…while
- The condition is checked after each iteration.for (;;)