0. String concatenation
console.log(`string literals :
''''
1+2=${1+2}`);
1. Increment and decrement operators
let counter = 2;
const preIncrement = ++counter;
counter = counter + 1;
preIncrement = counter;
const postIncrement = counter++;
postIncrement = counter;
counter = counter + 1;
2. Logical operators
|| or : finds the first truthy value
value1 || value2 || check() // true 는 뒤로!!
&& and : finds the first falsy value
if (nullableObject != null) {
nullableObject.something;
}
! not : change boolean
3. Equality
const stringFive = '5';
const numberFive = 5;
loose equality, with type converstion
stringFive == numberFive
stringFive != numberFive
strict equality, no type converstion //웬만하면 이거~
stringFive === numberFive
stringFive !== numberFive
+ object equality by reference
const a1 = {name : 'a'};
const a2 = {name : 'a'};
const a3 = a1;
console.log(a1 == a2); f //다른 reference 저장
console.log(a1 === a2); f //reference 값 다름
console.log(a1 === a3); t
+ quiz
0 == false T
0 === false F
' ' == false T
' ' === false F
null == undefined T
null === undefined F
4. Ternary operator : ?
// condition ? value1 : value2
const name = 'b'
console.log(name === 'a' ? 'yes' : 'no');
5. Switch
use for multiple if checks
use for enum-like value check
use for multiple type checks in TS
const browser = 'IE';
switch (browser) {
case 'IE':
console.log('a');
break;
case 'Chrome':
console.log('b');
break;
default:
console.log('c');
break;
}
6. Loops (while/ do/ for)
- while
let i = 3;
while (i>0) {
console.log(`while:${i}`);
i--;
}
- do // 블록 실행 후 조건 맞는지 안맞는지 검사. 블록 먼저 실행하고 싶을 때
do {
console.log(`while:${i}`);
i--;
} while (i>0);
- for
for (i=3; i>0; i--) { //let i=3 으로 해도 좋음
console.log(`for:${i}`);
}
+ nested loops
for (let i=0; i<10; i++) {
for (let i=0; i<10; i++) {
console.log(`i:${i}, j:${j}`);
}
}
→ O(n**2) 은 CPU에 좋지 않음
7.break vs continue
break : 루프 완전 끝냄
continue : 다음 스탭으로 넘어감.
+ quiz
'Programming > JavaScript' 카테고리의 다른 글
JS 문법 정리 (6) (0) | 2023.08.24 |
---|---|
JS 문법 정리 (5) (0) | 2023.08.22 |
JS 문법 정리 (4) (0) | 2023.08.18 |
JS 문법 정리 (3) (1) | 2023.08.15 |
JS 문법 정리 (0) | 2023.08.14 |