operator

  1. string concatenation
console.log('my'+' cat');
console.log('1'+2);  //12
console.log(`string literals : 1 + 2 = ${1 + 2}`);
  • ๊ธฐํ˜ธ๋ฅผ ์ด์šฉํ•ด์„œ ๋ฌธ์ž์—ด์„ ํ•ฉ์น  ์ˆ˜ ์žˆ๋‹ค

๋ฌธ์ž์—ด+๋ฌธ์ž์—ด = ๋ฌธ์ž์—ด ์ถœ๋ ฅ ๋ฌธ์ž์—ด+์ˆซ์ž = (์ˆซ์ž๋ฅผ ๋ฌธ์ž์—ด ํ™”)๋ฌธ์ž์—ด ์ถœ๋ ฅ

` ` ๋ฐฑํ‹ฑ์œผ๋กœ string literals์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์ค‘๊ฐ„์— ๋ณ€์ˆ˜๊ฐ€ ์‚ฝ์ž…๋˜๋ฉด ๊ณ„์‚ฐํ•ด์„œ ๋ฌธ์ž์—ด๋กœ ๋Œ๋ ค์ค€๋‹ค ์ค„๋ฐ”๊ฟˆ์ด๋‚˜ ํŠน์ˆ˜๊ธฐํ˜ธ๋„ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅ์ด ๊ฐ€๋Šฅ (์‹ฑ๊ธ€์ฟผํŠธ โ€˜ โ€˜ ์˜ ์‚ฌ์ด์— โ€˜๋ฅผ ๋„ฃ์„ ๋•Œ๋Š” ' ๋ฐฑ์Šฌ๋Ÿฌ์‹œ๋ฅผ ๋„ฃ์–ด์•ผ๋งŒ ํ•œ๋‹ค)

  1. Numeric operators ```js console.log(1 + 1); //add console.log(1 - 1); //substract console.log(1 / 1); //divide console.log(1 * 1); //multiply console.log(5 % 2); //reminder ๋‚˜๋ˆ„๊ณ  ๋‚˜๋จธ์ง€ console.log(2 ** 3); //exponentiation 2์˜ 3์Šน

3. Increment and decrement operators

```js
let counter = 2;

const preIncrement = ++counter;
// ์œ„ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๋™์ผํ•˜๋‹ค
//couter = counter + 1;
//preIncrement = counter;
// counter์— 1์„ ๋”ํ•ด์„œ ๊ฐ’์„ ๋‹ค์‹œ counter์— ํ• ๋‹นํ•œ ๋‹ค์Œ์— preIncrement์— counter์˜ ๊ฐ’์„ ํ• ๋‹นํ•œ๋‹ค

console.log(preIncrement, counter);
// 3,3 ์ถœ๋ ฅ

const postIncrement = counter++;
// ์œ„ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๋™์ผํ•˜๋‹ค
// postIncrement = counter;
counter = counter + 1;
// postIncrement์— counter์˜ ๊ฐ’์„ ํ• ๋‹นํ•œ ๋’ค์—, counter์— 1์„ ๋”ํ•ด์„œ ๊ฐ’์„ ๋‹ค์‹œ counter์— ํ• ๋‹นํ•œ๋‹ค
console.log(postIncrement, counter);
// 3,4 ์ถœ๋ ฅ

  1. Assignment operators
    let x = 3;
    let y = 6;
    x += y; // x = x + y;
    x -= y;
    x *= y;
    x /= y;
    
  2. Comparison operators
    console.log(10 < 6);
    console.log(10 <= 6);
    console.log(10 > 6);
    console.log(10 >= 6);
    
  3. logical operators : ใ…ฃใ…ฃ(or) , &&(and) , !(not)

const value1 = false;
const value2 = 4 < 2;

console.log(`or: ${ value1 ใ…ฃใ…ฃvalue2ใ…ฃใ…ฃcheck() }`); //or : true

console.log(`and: ${ value1 && value2 && check() }`); //and : false

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

  • or : finds the first truthy value

์ฒ˜์Œ์œผ๋กœ true๊ฐ€ ๋‚˜์˜ค๋ฉด ๊ฑฐ๊ธฐ์„œ ๋ฉˆ์ถ˜๋‹ค. ๋งŒ์•ฝ value1์ด true๋ผ๋ฉด ๋’ค๊ฐ€ ๋ญ๋“  ์ƒ๊ด€์—†์Œ. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ check๊ฐ™์€ expression์„ ์ œ์ผ ์•ž์— ๋‘๋ฉด ์•ˆ๋จ.

=> ๊ฐ„๋‹จํ•œ value๋“ค์„ ์ œ์ผ ์•ž์—๋‘ฌ์„œ ๊ฑ”๋“ค์ด false์ผ๋•Œ๋งŒ ๋งˆ์ง€๋ง‰์— ๋งˆ์ง€๋ชปํ•ด ํ˜ธ์ถœํ•˜๋Š”๊ฒŒ ์ œ์ผ ์ข‹๋‹ค.

  • and : finds the first falsy value ๋ชจ๋‘ true์ผ ๋•Œ๋งŒ true๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. or๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์•ž์˜ value๊ฐ€ false๋ผ๋ฉด ๊ฑฐ๊ธฐ์„œ ๋ฉˆ์ถ˜๋‹ค. ๋’ค๊ฐ€ ๋ญ๋“  ์ƒ๊ด€์—†์ด ์•„์˜ˆ ์‹คํ–‰์กฐ์ฐจ ํ•˜์ง€ ์•Š์Œ

=> and๋˜ํ•œ heavyํ•œ operation์ผ์ˆ˜๋ก ์ œ์ผ ์•ž์—์„œ ์ฒดํฌํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค

offen used to compress long if-statement ์ด๋Ÿฌํ•œ ์†์„ฑ ๋•Œ๋ฌธ์—, null ์ฒดํฌ๋ฌธ์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค

nullableObject && nullableObject.something
  • ๋งจ ์ฒ˜์Œ์ด false๋ผ๋ฉด ๋’ค๋ฅผ ์‹คํ–‰์กฐ์ฐจ ํ•˜์ง€์•Š๋Š” ์†์„ฑ์„ ์ด์šฉํ•œ ๊ฐ„๋‹จํ•œ null์ฒดํฌ๋ฌธ.
  • nullableObject๊ฐ€ null์ด ๋“ค์–ด์˜ค๋ฉด false์ด๊ธฐ ๋•Œ๋ฌธ์—, ๋’ค๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค!

  • not : !
console.log(!value1);

๊ฐ’์„ ๋ฐ˜๋Œ€๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค

  1. Equality
const stringFive = '5';
const numberFive = 5;

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

// string equality, no type conversion
console.log(stringFive === numberFive); // false
console.log(stringFive !== numberFive); // true
  • == / !== loose equality
    ํƒ€์ž…์„ ๋ณ€๊ฒฝํ•ด์„œ ๊ฐ’์„ ๋น„๊ตํ•œ๋‹ค
  • === / !=== strick equality
    ํƒ€์ž…์„ ์—„์ˆ˜ํ•ด์„œ ๊ฐ’์„ ๋น„๊ตํ•œ๋‹ค
  • 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 === ellie2);// true

object๋Š” ๋ฉ”๋ชจ๋ฆฌ์— ํƒ‘์žฌ๋  ๋•Œ, reference ํ˜•ํƒœ๋กœ ์ €์žฅ๋œ๋‹ค

object ellie1๊ณผ ellie2๋Š” ๋“ค์–ด์žˆ๋Š” ๊ฐ’์€ ๊ฐ™์•„๋ณด์ด์ง€๋งŒ,
์‹ค์ œ๋กœ 1๊ณผ 2์—๋Š” ๊ฐ๊ฐ ๋‹ค๋ฅธ ๋ ˆํผ๋Ÿฐ์Šค๊ฐ€ ์ €์žฅ๋˜์–ด์žˆ์Œ
๊ทธ ๋‹ค๋ฅธ ๋ ˆํผ๋Ÿฐ์Šค๋Š”, ์„œ๋กœ ๋‹ค๋ฅธ object๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋‹ค
ellie3๋Š” ellie1๊ณผ ๋™์ผํ•œ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ์Œ.

๊ทธ๋ž˜์„œ ellie1๊ณผ ellie2๋Š” ํƒ€์ž…์„ ๋™์ผํ•˜๊ฒŒ ๋ณธ๋Œ€๋„ ๋ ˆํผ๋Ÿฐ์Šค๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ™์ง€ ์•Š๋‹ค!
ํƒ€์ž…์„ ์—„์ˆ˜ํ•ด์„œ ๋น„๊ตํ•ด๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ ˆํผ๋Ÿฐ์Šค๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ™์ง€ ์•Š์Œ.
ํ•˜์ง€๋งŒ ellie3๊ณผ ellie1๋Š” ๋™์ผํ•œ ๋ ˆํผ๋Ÿฐ์Šค์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ™๋‹ค!

  • equality puzzler *
console.log(0 == false); // true
console.log(0 === false); // false 0์€ boolean์ด ์•„๋‹ˆ๋‹ค
console.log('' == false); // true empty๋ฌธ์ž์—ด์€ false์ด๋‹ค
console.log('' === false); // false empty ๋ฌธ์ž์—ด์€ boolean์ด ์•„๋‹ˆ๋‹ค
console.log(null == undefined); //true null๊ณผ undefined์€ ๊ฐ™๋‹ค (ํŠน์ดํ•˜๊ฒŒ๋„)
console.log(null === undefined); //false null๊ณผ undefined๋Š” ๋‹ค๋ฅธ ํƒ€์ž…์ด๋‹ค
  1. Conditional operator: if //if,else,lf,else
const name = 'ellie';
if(name === 'ellie') {
  console.log('Welcome, Ellie!');
}else if(name === 'coder') {
  console.log('You are amazing coder');
}else {
  console.log('unknown');
}

if statement๊ฐ€ true๋ผ๋ฉด, ๊ทธ ์•ˆ์— ์žˆ๋Š” block์„ ์‹คํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค
๊ทธ๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด else if, ๊ทธ๊ฒƒ๋„ ์•„๋‹ˆ๋ผ๋ฉด else๋กœ ๋„˜์–ด์™€์„œ block์„ ์‹คํ–‰ํ•œ๋‹ค

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

๊ธธ์–ด์ง€๋ฉด ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์—, ์กฐ๊ฑด๋ฌธ์ด ๊ฐ„๋‹จํ•  ๋•Œ์—๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

  1. 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':
  //   console.log('love you!');
  //   break;
  // case 'FireFox':
  //   console.log('love you!');
  //   break;
  case 'Chrome':
  case 'FireFox':
    console.log('love you!');
    break;
  default:
    console.log('love you!');
    break;
}

if๋ฌธ์—์„œ else if๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค๋ฉด switch๋ฅผ ์“ฐ๋Š” ๊ฒƒ์„ ๊ณ ๋ คํ•˜๋Š” ๊ฒŒ ์ข‹๋‹ค!

๋งŒ์•ฝ ์‹คํ–‰ํ•  ๋™์ž‘์ด ๊ฐ™์€ ๊ฒฝ์šฐ์—” case๋ฅผ ์—ฐ๋‹ฌ์•„ ์“ฐ๋ฉด ๋œ๋‹ค

TS์—์„œ ์ •ํ•ด์ ธ์žˆ๋Š” ํƒ€์ž…์„ ๊ฒ€์‚ฌํ•  ๋•Œ์— switch๋ฅผ ์“ฐ๋Š” ๊ฒƒ์ด ๊ฐ€๋…์„ฑ์ด ์ข‹๋‹ค

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

while์˜ statement๊ฐ€ false๋กœ ๋‚˜์˜ค๊ธฐ ์ „๊นŒ์ง€๋Š” ๋ฌดํ•œ๋Œ€๋กœ ๋ฐ˜๋ณตํ•ด์„œ ๊ณ„์† ๋ˆ๋‹ค.

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

๋จผ์ € ๋ธ”๋Ÿญ์„ ์‹คํ–‰ํ•œ ํ›„์— ์กฐ๊ฑด์„ ๊ฒ€์‚ฌํ•œ๋‹ค.

=> ๋ธ”๋Ÿญ์„ ๋จผ์ € ์‹คํ–‰ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด do while,
์กฐ๊ฑด์— ๋งž์„ ๋•Œ์—๋งŒ ๋ธ”๋Ÿญ์„ ์‹คํ–‰ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด while์„ ์จ์•ผํ•œ๋‹ค!

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

for์€ ์‹œ์ž‘ํ•˜๋Š” ๋ฌธ์žฅ, ์ปจ๋””์…˜, ์–ด๋–ค ์Šคํ…์„ ๋ฐŸ์„ ๊ฒƒ์ธ์ง€๋ฅผ ๋ช…์‹œํ•œ๋‹ค
1) begin์„ ์ฒ˜์Œ์— ๋”ฑ ํ•œ๋ฒˆ๋งŒ ์‹คํ–‰ํ•˜๊ณ 
2) ๋ธ”๋Ÿญ์„ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— ์ปจ๋””์…˜์ด ๋งž๋Š”์ง€ ๊ฒ€์‚ฌํ•œ ๋‹ค์Œ
3) ๋ธ”๋Ÿญ์„ ์‹คํ–‰ํ•˜๊ณ 
4) ์Šคํ…์„ ์‹คํ–‰ํ•œ๋‹ค

=> ์ปจ๋””์…˜์ด ๋งž์„ ๋™์•ˆ 2,3,4๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค

์ด for๋ฌธ์ฒ˜๋Ÿผ ๊ธฐ์กด์— ์กด์žฌํ•˜๋Š” ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ํ• ๋‹นํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๊ณ 

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

์ด๋ ‡๊ฒŒ for์•ˆ์—์„œ, block์•ˆ์— let์œผ๋กœ ์ง€์—ญ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด์„œ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

nested loops

  • while๊ณผ for์€ nested loops์ด ๊ฐ€๋Šฅํ•˜๋‹ค
for (let i = 0; i < 10; i++) {
  for (let j = 0; i < 10; i++) {
    console.log(`i: ${i}, j: ${j}`);
  }
}

for๋ฌธ ์†์˜ for๋ฌธ์˜ ์ˆœ์„œ 1) i๊ฐ€ 0์ผ ๋•Œ, j๋Š” 0~9๊นŒ์ง€ ๋ฐ˜๋ณต๋˜๊ณ 
2) i๊ฐ€ 1์ผ ๋•Œ, j๋Š” 0~9๊นŒ์ง€ ๋ฐ˜๋ณต๋˜๊ณ 
โ€ฆ..
ex)๊ตฌ๊ตฌ๋‹จ

  • break, continue
    loop ์•ˆ์—์„œ๋Š” break์™€ continue๋ฅผ ์ด์šฉํ•ด ๋ฃจํ”„๋ฅผ ๋๋‚ผ ์ˆ˜ ์žˆ๋‹ค!

  • break
    ๋ฃจํ”„๋ฅผ ์™„์ „ํžˆ ๋๋ƒ„
  • continue
    ์ง€๊ธˆ ๊ฒƒ๋งŒ ์Šคํ‚ตํ•˜๊ณ , ๋‹ค์Œ ์Šคํ…์œผ๋กœ ๋„˜์–ด๊ฐ

Q1. iterate from 0 to 10 and print only even numbers (use continue)


for(let i = 0; i < 11; i++) {
  if(i % 2 !== 0) {
    continue;
    // 2๋กœ ๋‚˜๋ˆด์„ ๋•Œ 0์ด ์•„๋‹ˆ๋ฉด ์Šคํ‚ตํ•˜๊ณ  ๋‹ค์Œ ์Šคํ…์œผ๋กœ
  }
    console.log(`even number is : ${i}`)
}
// ์‹ค๋ฌด์ ์ธ ๋‹ต :
// for(let i = 0; i < 11; i++) {
//   if(i % 2 == 0 && i > 0) {
//       console.log(`even number is : ${i}`)
//   }
// }


Q2. iterate from 0 to 10 and print numbers until reaching 8 (use break)


for(let i = 0; i < 11; i++) {
  if(i == 8){
    break;
  }
  console.log(`number is : ${i}`);
}