연산 and 반복문

유호익·2021년 1월 3일
0

js

목록 보기
3/3

Operator

1. String concatenation

  • console.log(`string literals: ''''
    1 + 2 = ${1 + 2}`);
    -> ${}안에 있는것만 연산 그 외 띄어쓰기와 줄바꿈도 표현된다
    \을 앞에 붙이면 ` 백틱도 나오게 할 수 있다.

  • console.log("hoic's \n\tbook");
    -> \n 은 줄바꿈 \t는 탭으로 출력된다.

2. Numberic operator

  • console.log(1 + 1); -> add
  • console.log(1 - 1); -> subtract
  • console.log(1 * 1); -> multiply
  • console.log(1 / 1); -> divide
  • console.log(5 % 2); -> remainder
  • console.log(2 ** 3); -> exponentiation -> 2의 3승

3. Increment and decrement operators

let counter = 2;

  • const preIncrement = ++counter;
    이것은 풀면
    counter = counter + 1;
    preIncrement = counter; 같이 해석되고

  • let counter = 2;
    const postIncrement = counter++;
    이것을 풀면
    postIncrement = counter;
    counter = counter + 1; 같이 해석된다.

4. Assingnment operators

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

5. Comparision operators

  • console.log(1 < 1); -> less than
  • console.log(1 <= 1); -> less than or equal
  • console.log(1 > 1); -> greater than
  • console.log(1 >= 1); -> greater than or equal

6. Logical operators: ||(or), &&(and), !(not)

  • or 연산자에선 조건 중 하나만 맞아도 true가 반환된다.
    조건 중 가벼운 것일수록 조건을 앞에 써주어서 무거운 조건은 읽지 않게끔 하자

  • and 연산자는 반대로 조건 하나만 달라도 false를 반환하므로
    조건 중 가벼운 것일수록 조건을 앞에 써주어서 무거운 조건은 후에 읽게 하자

7. Equality

  • == loose eauality, with type converstion
  • === strict equality, no type coversion

8. Conditional operators

if, else if, else

9. Ternary operator: ?

condition ? value1 : value2;
console.log(name === 'hoic' ? 'yes' : 'no');
조건 ? 값1 : 값2

10. Switch statement

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!);
}

11. Loops

  • 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 만들기!

profile
There's more to do than can ever be done

0개의 댓글