22 Dec Nullish coalescing operator, Loop

sunghoon·2024년 12월 22일
0

2.0 Glove Project

목록 보기
3/35
post-thumbnail

2.12 Nullish coalescing operator ‘??’

  • The nullish coalescing operator is written as two question marks ??.
    • for brevity
result = a ?? b 
// result = (a !== null && a !== undefined) ? a : b;
alert(firstName ?? lastName ?? nickName ?? "Anonymous"); // Supercoder
// alert(firstName || lastName || nickName || "Anonymous");
  • Comparison with ||
    • The height || 100 checks height for being a falsy value, and it’s 0, falsy indeed.
  • Precedence
    • forbids using ?? together with && and || operators, unless the precedence is explicitly specified with parentheses.

2.13 Loops: while and for

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(;;)
  • for statement semicolons must be present
  • Breakning the loop break
let sum = 0;
while () {
    let value = +prompt("Enter a number", '');
	if (!value) break;
	sum += value;
}
alert('Sum' + sum);

+prompt("Enter a number", '');

  • Numberic conversion, unary + - link
  • prompt- link
  • continue to the next iteration
    • continue stops the current iteration and start new one.
      It doesn’t stop the whole loop
  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:

  • Summary
    while
    do…while - The condition is checked after each iteration.
    for (;;)

others

  • extention
    - open in browser - link
    - Alt + B
    - live server - link
    - Alt + L / Alt + O
  • Chrome TTS
    - Speechify Text to Speech Voice Reader - link
    • Read Aloud: A Text to Speech Voice Reader - link
profile
프라다 신은 빈지노와 쿠페를 타는 꿈을 꿨다.

0개의 댓글