프로그래밍/JavaScript

[자바스크립트] operator, if, for loop, 코드리뷰 팁

choar 2021. 3. 21. 19:36
반응형

드림코딩 엘리 - 자바스크립트 강의 #4

 

 

1. String concatenation

  • console.log('my' + ' cat'); ⇒ my cat
  • console.log('1' + 2); ⇒ 12
  • console.log(${1+2}); ⇒ 3
  • \n \t 등등 ..

2. Numeric operators

  • exponentiation : **

3. Increment and decrement operators

let counter = 2;
const preIncrement = ++counter; // 3
const postIncrement = counter++; // 3 (그후에 4됨)

// --counter, counter--

4. Assignment operators

=, +=, -=, *=, /=

5. Comparison operators

<, <=, >, >=

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

  • || (or), finds the first truthy value true가 앞에서 하나 나오면 뒤에거 확인 안함
    ⇒ 연산 많이하거나 함수 호출하는 애를 맨 뒤에다 놔야함
  • && (and), finds the first falsy value false가 앞에서 하나 나오면 뒤에거 확인 안함
    ⇒ heavy할수록 뒤에 놓기
  • often used to compress long if-statement nullableObject && nullableObject.something
if (nullableObject != null) {
	nullableObject.something;
} // ⇒ null이 아닐때만 something 받아온다
  • ! (not)

7. Equality

  • const stringFive = '5'; const numberFive = 5;
  • == loose equality, with type conversion
  • stringFive == numberFive ⇒ true stringFive != numberFive ⇒ false
  • === strict equality, no type conversion
  • stringFive === numberFive ⇒ false stringFive !== numberFive ⇒ true
  • object equality by reference (object는 무조건 reference 비교)
const ellie1 = {name: 'ellie'};
const ellie2 = {name: 'ellie'};
const ellie3 = ellie1;
console.log(ellie1 == ellie2); // false (reference 다름)
console.log(ellie1 === ellie2); // false
console.log(ellie1 === ellie3); // true

  • equality - puzzler
console.log(0 == false); // true
console.log(0 === false); // false
console.log('' == false); // true 
console.log('' === false); // false 
console.log(null == undefined); // **true** 
console.log(null == undefined); // false

8. Conditional operators: if

  • if, else if, else

9. Ternary Operator: ?

  • condition ? value1 : value2;
  • condition이 true면 value1, false면 value2

10. Switch statement

  • use for multiple if checks
  • use for enum-like value check
  • use for multiple type checks in TS
  • switch, case, break, default

11. Loops

  • while loop : while the condition is truthy, body code is executed.
  • do while loop : body code is executed first, then check the condition.
  • for loop : for (begin; condition; step) begin은 1번만 실행 → condition 확인 → step → condition → step → ...
  • nested loops
  • break, continue
    1. Iterate from 0 to 10 and print only even numbers (use continue)
    2. iterate from 0 to 10 and print numbers until reaching 8 (use break)

 

반응형