개발 공부/자바스크립트

[엘리의 드림코딩 JS] 4. 연산(operator)

5묘 2022. 12. 4. 10:12

https://youtu.be/YBjufjBaxHo

// 1. string concatenation
console.log('my' + ' cat');
console.log('1' + 2);
console.log(`string literals: 1 + 2 = ${1 + 2}`);
console.log('ellie\'s book');


// 2. Numeric operators
console.log(1 + 1); // add
console.log(1 - 1); // substract
console.log(1 / 1); // divide
console.log(1 * 1); // multiply
console.log(5 % 2); // remainder
console.log(2 ** 3); // exponentiation


// 3. Increment and decrement operators
let counter = 2;
const preIncrement = ++counter;
console.log(preIncrement);
// counter = counter + 1;
// preIncrement = counter;

const postIncrement = counter++;
console.log(postIncrement);
// postIncrement = counter;
// counter = counter + 1;

const preDecrement = --counter; 
console.log(preDecrement);
// counter = counter - 1;
// preDecrement = counter;

const postDecrement = counter--;
console.log(postDecrement);
// postDecrement = counter;
// counter = counter - 1;


// 4. Assignment operators
let x = 3;
let y = 6;
x += y; // x = x+y;
x -= y;
x *= y;
x /= y;


// 5. Comparison operators
console.log(10 < 6); //less than
console.log(10 <= 6); //less than or equal
console.log(10 > 6); //greater than
console.log(10 >= 6); //greater than or equal


// 6. logical operators : ||(or), &&(and), !(not)
const value1 = false;  // true로 바꾸면 단락평가돼서 check() 함수 실행 안됨.
const value2 = 4 < 2;

// || (or) : finds the first truthy value
// 따라서, 함수처럼 시간 많이 걸리는 애는 뒤에 배치해줘야 한다!
console.log(`or: ${value1 || value2 || check()}`);

// &&(and) : finds the first falsy value
// often used to compress long if-statement
// nullableObject && nullableObject.something
console.log(`and: ${value1 && value2 && check()}`);

function check() {
  for (let i=0; i < 10; i++) {
    //waisting time
    console.log('🎅');
  }
  return true;
}

// !(not) 반대로 바꿈
const my = true;
console.log(!my);


// 7. Equality
const stringFive = '5';
const numberFive = 5;

// == loose equality, with type conversion
console.log(stringFive == numberFive);
console.log(stringFive != numberFive)

// === strict equality, no type conversion
console.log(stringFive === numberFive);
console.log(stringFive !== numberFive)

// object equality by reference
const ellie1 = { name: 'ellie' };
const ellie2 = { name: 'ellie' };
const ellie3 = ellie1;
console.log(ellie1 == ellie2); //false
console.log(ellie1 === ellie2); //false
console.log(ellie1 === ellie3); //true. 같은 reference value를 할당했으므로

// equality - puzzler
console.log(0 == false); // true. boolean이 숫자로 바뀜
console.log(0 === false); // false. 타입변환X
console.log('' == false); // true. 1차로 false가 0으로 변환, 2차로 빈 문자열 0으로 변환
console.log('' === false); // false. 타입변환X
console.log(null == undefined); //true. 느슨한 동등비교 시 둘은 같은 것으로 취급됨.(다른 falsy값은 X)
console.log(null === undefined);// false. 타입변환X


// 8. Conditional Operators : if
// if, else if, else
const name = 'May';
if (name === 'May') {
  console.log(`Welcome, ${name}!`);
} else if (name === 'coder') {
  console.log(`You are amazing ${name}`);
} else {
  console.log('unknown');
}


// 9. Ternary operator: ?
// condition ? value1 : value2;
console.log(name === 'May' ? 'yes' : 'no');


// 10. Switch statement
// 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('go away!');
    break;  
  case 'Chrome':
  case 'Firefox': // 조건에 대해 실행되는 함수 같으면 이렇게 묶을 수 있다.
    console.log('love you!');
    break;
  default :
    console.log('same all!');
    break;
}


// 11. Loops
// while loop, while the condition is thruthy,
// body code is executed.
let i = 3;
while (i > 0) {
  console.log(`while: ${i}`);
  i--;
}

// do while loop, body code is executed first,
// then check the condition.
do {
  console.log(`do while: ${i}`);
  i--;
} while (i > 0);

// for loop, for(begin; condition; step)
for (i = 3; i > 0; i--) {
  console.log(`for: ${i}`);
}

for (let i = 3; i > 0; i -= 2){
  // inline variable declaration
  console.log(`inline variable for: ${i}`);
}

// nested loops (O(n^2) => 피하는 게 좋음)
for (let i = 0; i < 10; i++){
  for (let j = 0; j < 10; j++) {
    console.log(`i: ${i}, j: ${j}`);
  }
}

// break, continue
// Q1. iterate from 0 to 10 and print only even numbers
// (use continue)
for (let i = 0; i <= 10; i++) {
  if (i % 2 !== 0) {
    continue;
  }
  console.log(i);
}

// Q2. iterate from 0 to 10 and print numbers until 
// reaching 8 (use break)
let s = 0
while (s <= 10) {
  console.log(s);
  s++;
  if (s > 8) {
    break;
  }
}