Operator
console.log(`string literals: ''''
1 + 2 = ${1 + 2}`);
-> ${}안에 있는것만 연산 그 외 띄어쓰기와 줄바꿈도 표현된다
\을 앞에 붙이면 ` 백틱도 나오게 할 수 있다.
console.log("hoic's \n\tbook");
-> \n 은 줄바꿈 \t는 탭으로 출력된다.
let counter = 2;
const preIncrement = ++counter;
이것은 풀면
counter = counter + 1;
preIncrement = counter; 같이 해석되고
let counter = 2;
const postIncrement = counter++;
이것을 풀면
postIncrement = counter;
counter = counter + 1; 같이 해석된다.
let x = 3;
let y = 6;
x += y // x = x + y
x -= y // x = x - y
x *= y // x = x * y
x /= y // x = x / y
or 연산자에선 조건 중 하나만 맞아도 true가 반환된다.
조건 중 가벼운 것일수록 조건을 앞에 써주어서 무거운 조건은 읽지 않게끔 하자
and 연산자는 반대로 조건 하나만 달라도 false를 반환하므로
조건 중 가벼운 것일수록 조건을 앞에 써주어서 무거운 조건은 후에 읽게 하자
if, else if, else
condition ? value1 : value2;
console.log(name === 'hoic' ? 'yes' : 'no');
조건 ? 값1 : 값2
use for multiple if checks
use for enum-like value check
use for multiple type checks in TS
ex)
const hoic = 'goodman';
switch (hoic) {
case 'badman' : alert('it's not true!);
break;
case 'youngman' :
case 'ugly' : alert('it's not true!);
break;
case 'goodman' : alert('that's right!);
}
while loop
while (i > 0) {
consle.log(`while${i}`}
i--;
}
do while loop
do {
consle.log(`dowhile${i}`}
i--;
} while(i > 0}
먼저 실행시키고 조건문 실행
for loop
for(begin; condition; step)
ex)
for (i = 0; i < 10; i++) {
실행시킬것
}
inline variable declaration
for (let i = 0; i > 0; i = i - 2) {
실행시킬 것
}
- nested loops
for(let i = 0; i < 10; i++){
for(let j = 1; j < 10; j ++){
실행시킬 것
}}
continue, break 를 활용하여 loop operation 만들기!