๐Ÿณ๋ฒจ๋กœํผํŠธ์™€ ํ•จ๊ป˜ํ•˜๋Š” ๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋ณด๋ฉด์„œ ์ •๋ฆฌํ•œ ๊ธ€ ์ž…๋‹ˆ๋‹ค ๐Ÿณ

1. Hello JavaScript

  • ํฌ๋กฌ ๊ฐœ๋ฐœ์ž ๋„๊ตฌ์—์„œ Consoleํƒญ์„ ์—ด๊ณ  ์ฝ”๋“œ ์ž…๋ ฅํ•˜๊ธฐ

1) Hello JavaScript ์ถœ๋ ฅํ•˜๊ธฐ

console.log('Hello JavaScript!');
- ๊ฒฐ๊ณผ๊ฐ’ : Hello JavaScript!
ย 

2) ์—ฐ์‚ฐ

console.log(1 + 2 + 3 + 4);
- ๊ฒฐ๊ณผ๊ฐ’ : 10

ย 

2. ๋ณ€์ˆ˜์™€ ์ƒ์ˆ˜

1) ๋ณ€์ˆ˜

  • ๋ณ€์ˆ˜๋Š” ๋ฐ”๋€” ์ˆ˜ ์žˆ๋Š” ๊ฐ’
  • ๋ณ€์ˆ˜ ์„ ์–ธ ํ•  ๋•Œ์—๋Š” let ํ‚ค์›Œ๋“œ ์‚ฌ์šฉ
let value = 1;
console.log(value);
  • ๊ฒฐ๊ณผ๊ฐ’ : 1
    ย 
let value = 1;
console.log(value);
value = 2;
console.log(value);
  • ๊ฒฐ๊ณผ๊ฐ’
    1
    2
    ย 

2) ์ƒ์ˆ˜

  • ์ƒ์ˆ˜์˜ ๊ฐ’์€ ํ•œ๋ฒˆ ์„ ์–ธํ•œ ๋’ค ๋ฐ”๋€Œ์ง€ ์•Š์Œ.
  • ์ƒ์ˆ˜๋ฅผ ์„ ์–ธ ํ•  ๋•Œ์—๋Š” const ์‚ฌ์šฉ
const a = 1;

ย 

3) var

  • ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•
  • ๋˜‘๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ ์—ฌ๋Ÿฌ๋ฒˆ ์„ ์–ธ ๊ฐ€๋Šฅ
    ย 

4) ๋ฐ์ดํ„ฐ ํƒ€์ž…

์ˆซ์ž(Number) : ๋ฐ”๋กœ ๊ฐ’ ๋Œ€์ž… ๊ฐ€๋Šฅ

let value = 1;

ย 

๋ฌธ์ž์—ด(String) : ์ž‘์€ ๋”ฐ์˜ดํ‘œ ํ˜น์€ ํฐ ๋”ฐ์˜ดํ‘œ๋กœ ๊ฐ์‹ธ์„œ ์„ ์–ธ

let text = 'hello';
let name = '์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ';

ย 

์ฐธ/๊ฑฐ์ง“ (Boolean) : ์ฐธ์€ true, ๊ฑฐ์ง“์€ false

let value = 1;

ย 

null : ๊ฐ’์ด ์—†์„๋•Œ

const friend = null;

ย 

undefined : ์•„์ง ๊ฐ’์ด ์„ค์ •๋˜์ง€ ์•Š์€ ๊ฒƒ์„ ์˜๋ฏธ

let criminal;
console.log(criminal);

ย 

3. ์—ฐ์‚ฐ์ž

1) ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž

  • ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž๋Š” ์‚ฌ์น™์—ฐ์‚ฐ๊ณผ ๊ฐ™์€ ์ž‘์—…์„ ํ•˜๋Š” ์—ฐ์‚ฐ์ž๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    +: ๋ง์…ˆ
    -: ๋บผ์…ˆ
    *: ๊ณฑ์…ˆ
    /: ๋‚˜๋ˆ—์…ˆ
    ย 

2) ๋Œ€์ž… ์—ฐ์‚ฐ์ž

  • ๋Œ€์ž… ์—ฐ์‚ฐ์ž๋Š” ํŠน์ • ๊ฐ’์— ์—ฐ์‚ฐ์„ ํ•œ ๊ฐ’์„ ๋ฐ”๋กœ ์„ค์ • ํ•  ๋•Œ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋Š” ์—ฐ์‚ฐ์ž
let a = 1;
a += 3;
a -= 3;
a *= 3;
a /= 3;
console.log(a);
  • ๊ฒฐ๊ณผ๊ฐ’ : 1
    ย 

3) ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž

  • ๋ถˆ๋ฆฌ์–ธ ํƒ€์ž… (true ํ˜น์€ false)๋ฅผ ์œ„ํ•œ ์—ฐ์‚ฐ์ž
    !: NOT
    &&: AND
    ||: OR
    ย 

4) ์—ฐ์‚ฐ ์ˆœ์„œ

  • NOT -> AND -> OR
    ย 

5) ๋น„๊ต ์—ฐ์‚ฐ์ž

  • ๋‘ ๊ฐ’์„ ๋น„๊ต ํ•  ๋•Œ ์‚ฌ์šฉ

ย 

๋‘ ๊ฐ’์ด ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธ

'=' ๋ฌธ์ž 3๊ฐœ ์‚ฌ์šฉ
  • ์ผ์น˜ํ•œ๋‹ค๋ฉด, true, ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด false ๊ฐ€ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. (ํƒ€์ž… ๊ฒ€์‚ฌ๊นŒ์ง€)
const a = 1;
const b = 1;
const equals = a === b;
console.log(equals);
  • ๊ฒฐ๊ณผ๊ฐ’ : true
    ย 
'=' ๋ฌธ์ž 2๊ฐœ ์‚ฌ์šฉ
  • ์ผ์น˜ํ•œ๋‹ค๋ฉด, true, ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด false ๊ฐ€ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค.(ํƒ€์ž… ๊ฒ€์‚ฌ X)
const a = 1;
const b = 1;
const equals = a == b;
console.log(equals);

๊ฒฐ๊ณผ: true
ย 

ex1) ์ˆซ์ž 1๊ณผ ๋ฌธ์ž '1'์ด ๋™์ผํ•œ ๊ฐ’์œผ๋กœ ๊ฐ„์ฃผ
const a = 1;
const b = '1';
const equals = a == b;
console.log(equals);

๊ฒฐ๊ณผ: true
ย 

ex2) 0 ๊ณผ false ๋„ ๊ฐ™์€ ๊ฐ’์œผ๋กœ ๊ฐ„์ฃผ
const a = 0;
const b = false;
const equals = a == b;
console.log(equals);

๊ฒฐ๊ณผ: true
ย 

ex3) undefined ์™€ null ๋„ ๊ฐ™์€ ๊ฐ’์œผ๋กœ ๊ฐ„์ฃผ.
const a = null;
const b = undefined;
const equals = a == b;
console.log(equals);

๊ฒฐ๊ณผ: true
ย 

๋‘ ๊ฐ’์ด ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”์ง€ ํ™•์ธ

๋‘ ๊ฐ’์ด ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”์ง€ ํ™•์ธ ํ•  ๋•Œ์—๋Š” !== ๋ฅผ ์‚ฌ์šฉ
const value = 'a' !== 'b';

๊ฒฐ๊ณผ: true
ย 

!= ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํƒ€์ž… ๊ฒ€์‚ฌ X
console.log(1 != '1');
console.log(1 !== '1');

๊ฒฐ๊ณผ: true
ย 

ํฌ๊ณ  ์ž‘์Œ

๋‘ ๊ฐ’ ๋น„๊ต
const a = 10;
const b = 15;
const c = 15;

console.log(a < b); // true
console.log(b > a); // true
console.log(b >= c); // true
console.log(a <= c); // true
console.log(b < c); // false;

ย 

6) ๋ฌธ์ž์—ด ๋ถ™์ด๊ธฐ

+ ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด ๋ถ™์ด๊ธฐ

const a = '์•ˆ๋…•';
const b = 'ํ•˜์„ธ์š”';
console.log(a + b);

๊ฒฐ๊ณผ: ์•ˆ๋…•ํ•˜์„ธ์š”
ย 

4. ์กฐ๊ฑด๋ฌธ

1) if ๋ฌธ

if (์กฐ๊ฑด) {
  ์ฝ”๋“œ;
}
let ๊ณผ const ๋Š” ๋‹ค๋ฅธ ๋ธ”๋ก ๋ฒ”์œ„์—์„œ๋Š” ๋˜‘๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ ์„ ์–ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.
const a = 1;
if (true) {
  const a = 2;
  console.log('if๋ฌธ ์•ˆ์˜ a ๊ฐ’์€ ' + a);
}
console.log('if๋ฌธ ๋ฐ–์˜ a ๊ฐ’์€ ' + a);
  • ๊ฒฐ๊ณผ
"if๋ฌธ์˜ ์•ˆ์˜ a ๊ฐ’์€ 2"
"if๋ฌธ ๋ฐ–์˜ a ๊ฐ’์€ 1"

ย 

2) switch/case ๋ฌธ

const device = 'iphone';

switch (device) {
  case 'iphone':
    console.log('์•„์ดํฐ!');
    break;
  case 'ipad':
    console.log('์•„์ดํŒจ๋“œ!');
    break;
  case 'galaxy note':
    console.log('๊ฐค๋Ÿญ์‹œ ๋…ธํŠธ!');
    break;
  default:
    console.log('๋ชจ๋ฅด๊ฒ ๋„ค์š”..');
}

ย 

5. ํ•จ์ˆ˜

1) ํ•จ์ˆ˜

function ํ•จ์ˆ˜์ด๋ฆ„(๋งค๊ฐœ๋ณ€์ˆ˜) {
    ์ฝ”๋“œ
    return ์›ํ•˜๋Š” ๊ฐ’;
}

ย 

2) ํ™”์‚ดํ‘œ ํ•จ์ˆ˜

const ํ•จ์ˆ˜์ด๋ฆ„ = (๋งค๊ฐœ๋ณ€์ˆ˜) => {
    ์ฝ”๋“œ
    return ์›ํ•˜๋Š” ๊ฐ’;
}
  • function ํ‚ค์›Œ๋“œ ๋Œ€์‹ ์— => ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„, ํ™”์‚ดํ‘œ์˜ ์ขŒ์ธก์—๋Š” ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ, ํ™”์‚ดํ‘œ์˜ ์šฐ์ธก์—๋Š” ์ฝ”๋“œ ๋ธ”๋ก์ด ์˜จ๋‹ค.
ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์™€ ์ผ๋ฐ˜ function ์œผ๋กœ ๋งŒ๋“  ํ•จ์ˆ˜์™€์˜ ์ฃผ์š” ์ฐจ์ด์ ์€ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์—์„œ ๊ฐ€๋ฅดํ‚ค๋Š” this ์™€ function ์—์„œ ๊ฐ€๋ฅดํ‚ค๋Š” this ๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅด๋‹ค.

ย 

6. ๊ฐ์ฒด

  • ๋ณ€์ˆ˜ ํ˜น์€ ์ƒ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ๋ ๋•Œ ํ•˜๋‚˜์˜ ์ด๋ฆ„์— ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ๊ฐ’์„ ๋„ฃ์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.
    ย 

1) ๊ฐ์ฒด

  • ๊ฐ์ฒด๋ฅผ ์„ ์–ธ ํ•  ๋•Œ์—๋Š” ์ด๋ ‡๊ฒŒ { } ๋ฌธ์ž ์•ˆ์— ์›ํ•˜๋Š” ๊ฐ’๋“ค์„ ๋„ฃ๊ณ , ๋‚ด์šฉ์€ ํ‚ค: ์›ํ•˜๋Š”๊ฐ’์˜ ํ˜•ํƒœ๋กœ ๋„ฃ์–ด์•ผ ํ•œ๋‹ค.
  • ํ‚ค๋Š” ๊ณต๋ฐฑ์ด ์—†์–ด์•ผํ•˜๋ฉฐ, ์žˆ์–ด์•ผํ•œ๋‹ค๋ฉด ๋”ฐ์˜ดํ‘œ๋กœ ๊ฐ์‹ธ๊ธฐ
const sample = {
  'key with space': true
};

ย 

2) ํ•จ์ˆ˜์—์„œ ๊ฐ์ฒด๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๊ธฐ

const ironMan = {
  name: 'ํ† ๋‹ˆ ์Šคํƒ€ํฌ',
  actor: '๋กœ๋ฒ„ํŠธ ๋‹ค์šฐ๋‹ˆ ์ฃผ๋‹ˆ์–ด',
  alias: '์•„์ด์–ธ๋งจ'
};

const captainAmerica = {
  name: '์Šคํ‹ฐ๋ธ ๋กœ์ €์Šค',
  actor: 'ํฌ๋ฆฌ์Šค ์—๋ฐ˜์Šค',
  alias: '์บกํ‹ด ์•„๋ฉ”๋ฆฌ์นด'
};

function print(hero) {
  const text = `{hero.alias}({hero.name}) ์—ญํ• ์„ ๋งก์€ ๋ฐฐ์šฐ๋Š” ${
    hero.actor
  } ์ž…๋‹ˆ๋‹ค.`;
  console.log(text);
}

print(ironMan);
print(captainAmerica);

ย 

3) ๊ฐ์ฒด ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น

const ironMan = {
  name: 'ํ† ๋‹ˆ ์Šคํƒ€ํฌ',
  actor: '๋กœ๋ฒ„ํŠธ ๋‹ค์šฐ๋‹ˆ ์ฃผ๋‹ˆ์–ด',
  alias: '์•„์ด์–ธ๋งจ'
};

const captainAmerica = {
  name: '์Šคํ‹ฐ๋ธ ๋กœ์ €์Šค',
  actor: 'ํฌ๋ฆฌ์Šค ์—๋ฐ˜์Šค',
  alias: '์บกํ‹ด ์•„๋ฉ”๋ฆฌ์นด'
};

function print({ alias, name, actor }) {
  const text = `{alias}({name}) ์—ญํ• ์„ ๋งก์€ ๋ฐฐ์šฐ๋Š” ${actor} ์ž…๋‹ˆ๋‹ค.`;
  console.log(text);
}

print(ironMan);
print(captainAmerica);

ย 

4) ๊ฐ์ฒด ์•ˆ์— ํ•จ์ˆ˜ ๋„ฃ๊ธฐ

  • ํ•จ์ˆ˜๊ฐ€ ๊ฐ์ฒด์•ˆ์— ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๋ฉด, this ๋Š” ์ž์‹ ์ด ์†ํ•ด์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฅดํ‚จ๋‹ค.
  • ๊ฐ์ฒด ์•ˆ์— ํ•จ์ˆ˜๋ฅผ ๋„ฃ์„ ๋•Œ, ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋กœ ์„ ์–ธํ•œ๋‹ค๋ฉด ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค.
const dog = {
  name: '๋ฉ๋ฉ์ด',
  sound: '๋ฉ๋ฉ!',
  say: function() {
    console.log(this.sound);
  }
};

dog.say();

ย 

5) Getter ํ•จ์ˆ˜์™€ Setter ํ•จ์ˆ˜

  • Getter ํ•จ์ˆ˜์™€ Setter ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ํŠน์ • ๊ฐ’์„ ๋ฐ”๊พธ๋ ค๊ณ  ํ•˜๊ฑฐ๋‚˜, ํŠน์ • ๊ฐ’์„ ์กฐํšŒํ•˜๋ ค๊ณ  ํ•  ๋•Œ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.
onst numbers = {
  _a: 1,
  _b: 2,
  sum: 3,
  calculate() {
    console.log('calculate');
    this.sum = this._a + this._b;
  },
  get a() {
    return this._a;
  },
  get b() {
    return this._b;
  },
  set a(value) {
    console.log('a๊ฐ€ ๋ฐ”๋€๋‹ˆ๋‹ค.');
    this._a = value;
    this.calculate();
  },
  set b(value) {
    console.log('b๊ฐ€ ๋ฐ”๋€๋‹ˆ๋‹ค.');
    this._b = value;
    this.calculate();
  }
};

console.log(numbers.sum);
numbers.a = 5;
numbers.b = 7;
numbers.a = 9;
console.log(numbers.sum);
console.log(numbers.sum);
console.log(numbers.sum);
  • ๊ฒฐ๊ณผ
3
a๊ฐ€ ๋ฐ”๋€๋‹ˆ๋‹ค.
calculate
b๊ฐ€ ๋ฐ”๋€๋‹ˆ๋‹ค.
calculate
a๊ฐ€ ๋ฐ”๋€๋‹ˆ๋‹ค.
calculate
16
16
16
  • Setter ํ•จ์ˆ˜๋ฅผ ์„ค์ • ํ•  ๋•Œ์—๋Š” ํ•จ์ˆ˜์˜ ์•ž๋ถ€๋ถ„์— set ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์ธ๋‹ค. Setter ํ•จ์ˆ˜๋ฅผ ์„ค์ •ํ•˜๊ณ  ๋‚˜๋ฉด, numbers.a = 5 ์ด๋ ‡๊ฒŒ ๊ฐ’์„ ์„ค์ •ํ–ˆ์„ ๋•Œ 5 ๋ฅผ ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„์˜ค๊ฒŒ ๋œ๋‹ค. ์œ„ ์ฝ”๋“œ์—์„œ๋Š” ๊ฐ์ฒด ์•ˆ์— _a, _b ๋ผ๋Š” ์ˆซ์ž๋ฅผ ์„ ์–ธํ•ด์ฃผ๊ณ , ์ด ๊ฐ’๋“ค์„ ์œ„ํ•œ Getter ์™€ Setter ํ•จ์ˆ˜๋ฅผ ์„ค์ •ํ–ˆ๋‹ค.
    ย 

7. ๋ฐฐ์—ด

  • ๋ฐฐ์—ด์€ ์—ฌ๋Ÿฌ๊ฐœ์˜ ํ•ญ๋ชฉ๋“ค์ด ๋“ค์–ด์žˆ๋Š” ๋ฆฌ์ŠคํŠธ
  • ๋ฐฐ์—ด์„ ์„ ์–ธ ํ•  ๋•Œ์—๋Š” [ ] ์‚ฌ์šฉ.
    ย 

1) ๊ฐ์ฒด ๋ฐฐ์—ด ์„ ์–ธ

const objects = [{ name: '๋ฉ๋ฉ์ด' }, { name: '์•ผ์˜น์ด' }];

console.log(objects);
console.log(objects[0]);
console.log(objects[1]);
  • ๊ฒฐ๊ณผ

    ย 

2) ๋ฐฐ์—ด์— ์ƒˆ ํ•ญ๋ชฉ ์ถ”๊ฐ€ํ•˜๊ธฐ

const objects = [{ name: '๋ฉ๋ฉ์ด' }, { name: '์•ผ์˜น์ด' }];

objects.push({
  name: '๋ฉ๋ญ‰์ด'
});

console.log(objects);
  • ๊ฒฐ๊ณผ

    ย 

3) ๋ฐฐ์—ด์˜ ํฌ๊ธฐ ์•Œ์•„๋‚ด๊ธฐ

  • length ๊ฐ’์„ ํ™•์ธ
const objects = [{ name: '๋ฉ๋ฉ์ด' }, { name: '์•ผ์˜น์ด' }];

console.log(objects.length);

objects.push({
  name: '๋ฉ๋ญ‰์ด'
});

console.log(objects.length);
  • ๊ฒฐ๊ณผ
2
3

ย 

8. ๋ฐ˜๋ณต๋ฌธ

1) for

for (์ดˆ๊ธฐ ๊ตฌ๋ฌธ; ์กฐ๊ฑด ๊ตฌ๋ฌธ; ๋ณ€ํ™” ๊ตฌ๋ฌธ;) {
  ์ฝ”๋“œ
}

ย 

2) while

while (์กฐ๊ฑด ๊ตฌ๋ฌธ) {
  ์ฝ”๋“œ
}

ย 

3) for...of

  • for...of๋ฌธ์€ ๋ฐฐ์—ด์— ๊ด€ํ•œ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฆฌ๊ธฐ ์œ„ํ•ด์„œ ๋งŒ๋“ค์–ด์ง„ ๋ฐ˜๋ณต๋ฌธ
let numbers = [10, 20, 30, 40, 50];
for (let number of numbers) {
  console.log(number);
}

ย 

4) ๊ฐ์ฒด๋ฅผ ์œ„ํ•œ ๋ฐ˜๋ณต๋ฌธ for...in

  • ๊ฐ์ฒด์˜ ์ •๋ณด๋ฅผ ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ๋ฐ›์•„์˜ฌ ์ˆ˜์žˆ๋Š” ํ•จ์ˆ˜
Object.entries: [[ํ‚ค, ๊ฐ’], [ํ‚ค, ๊ฐ’]] ํ˜•ํƒœ์˜ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜
Object.keys: [ํ‚ค, ํ‚ค, ํ‚ค] ํ˜•ํƒœ์˜ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜
Object.values: [๊ฐ’, ๊ฐ’, ๊ฐ’] ํ˜•ํƒœ์˜ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜

๊ฐ์ฒด๊ฐ€ ์ง€๋‹ˆ๊ณ  ์žˆ๋Š” ๊ฐ’์— ๋Œ€ํ•˜์—ฌ ๋ฐ˜๋ณต์„ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์œ„ ํ•จ์ˆ˜๋“ค์„ ์‚ฌ์šฉ ๋˜๋Š” for...in ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉ

const doggy = {
  name: '๋ฉ๋ฉ์ด',
  sound: '๋ฉ๋ฉ',
  age: 2
};

for (let key in doggy) {
  console.log(`{key}:{doggy[key]}`);
}

ย 

5) ์—ฐ์Šต๋ฌธ์ œ

  • ์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ํ•ด๋‹น ์ˆซ์ž ๋ฐฐ์—ด์•ˆ์— ๋“ค์–ด์žˆ๋Š” ์ˆซ์ž ์ค‘ 3๋ณด๋‹ค ํฐ ์ˆซ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด์„ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์„œ ๋ฐ˜ํ™˜ํ•ด๋ณด์„ธ์š”.
function biggerThanThree(numbers) {
  const N = [];
  for(let i = 0; i <= numbers.length; i++){
    if (numbers[i] > 3) N.push(numbers[i]);
  }
  return N;
}

const numbers = [1, 2, 3, 4, 5, 6, 7];
console.log(biggerThanThree(numbers)); // ๊ฒฐ๊ณผ ๊ฐ’ [4, 5, 6, 7]

ย 

9. ๋ฐฐ์—ด ๋‚ด์žฅํ•จ์ˆ˜

1) forEach

function biggerThanThree(numbers) {
  const N = [];
  for(let i = 0; i <= numbers.length; i++){
    if (numbers[i] > 3) N.push(numbers[i]);
  }
  return N;
}

const numbers = [1, 2, 3, 4, 5, 6, 7];
console.log(biggerThanThree(numbers)); // ๊ฒฐ๊ณผ ๊ฐ’ [4, 5, 6, 7]

โฌ‡ forEach๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด

function biggerThanThree(numbers) {
  const N = [];
  numbers.forEach ( n => {
    if(n > 3) N.push(n);
  });
  return N;
}

const numbers = [1, 2, 3, 4, 5, 6, 7];
console.log(biggerThanThree(numbers)); // ๊ฒฐ๊ณผ ๊ฐ’ [4, 5, 6, 7]
  • forEach ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ๋Š”, ๊ฐ ์›์†Œ์— ๋Œ€ํ•˜์—ฌ ์ฒ˜๋ฆฌํ•˜๊ณ  ์‹ถ์€ ์ฝ”๋“œ๋ฅผ ํ•จ์ˆ˜๋กœ ๋„ฃ์–ด์คŒ. ์ด ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ hero๋Š” ๊ฐ ์›์†Œ๋ฅผ ๊ฐ€๋ฅดํ‚จ๋‹ค.
  • ํ•จ์ˆ˜ํ˜•ํƒœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์„ ์ฝœ๋ฐฑํ•จ์ˆ˜ ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ํ•จ์ˆ˜๋ฅผ ๋“ฑ๋กํ•ด์ฃผ๋ฉด, forEach ๊ฐ€ ์‹คํ–‰
    ย 

2) map

  • map ์€ ๋ฐฐ์—ด ์•ˆ์˜ ๊ฐ ์›์†Œ๋ฅผ ๋ณ€ํ™˜ ํ•  ๋•Œ ์‚ฌ์šฉ
function biggerThanThree(numbers) {
  const N = [];
  numbers.forEach ( n => {
    if(n > 3) N.push(n);
  });
  return N;
}

const numbers = [1, 2, 3, 4, 5, 6, 7];
console.log(biggerThanThree(numbers)); // ๊ฒฐ๊ณผ ๊ฐ’ [4, 5, 6, 7]

export default biggerThanThree;

โฌ‡ map์„ ์‚ฌ์šฉํ•˜๋ฉด

function biggerThanThree(numbers) {
  const N = numbers.map(n => {
    if(n>3) return n;
  }).filter(data => data);
  return N;
}
const numbers = [1, 2, 3, 4, 5, 6, 7];
console.log(biggerThanThree(numbers)); // ๊ฒฐ๊ณผ ๊ฐ’ [4, 5, 6, 7]

ย 

3) indexOf

  • indexOf๋Š” ์›ํ•˜๋Š” ํ•ญ๋ชฉ์ด ๋ช‡๋ฒˆ์งธ ์›์†Œ์ธ์ง€ ์ฐพ์•„์ฃผ๋Š” ํ•จ์ˆ˜
const dogs = ['์ฟ ํ‚ค', '์ฟ ๋ง', '์ฟ ๋กฑ', '์ƒˆ์šฐ'];
const index = dogs.indexOf('์ฟ ๋กฑ');
console.log(index); // ๊ฒฐ๊ณผ ๊ฐ’ 2

ย 

4) findIndex

  • indexOfํ•จ์ˆ˜๋กœ๋Š” ๋ฐฐ์—ด ์•ˆ์— ์žˆ๋Š” ๊ฐ’์ด ๊ฐ์ฒด์ด๊ฑฐ๋‚˜, ๋ฐฐ์—ด์ด๋ผ๋ฉด ์ฐพ์„ ์ˆ˜ ์—†๋‹ค -> ์ด๋Ÿด๋• findIndex๋ฅผ ์‚ฌ์šฉ !
const todos = [
  {
    id: 1,
    text: '์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ž…๋ฌธ',
    done: true
  },
  {
    id: 2,
    text: 'ํ•จ์ˆ˜ ๋ฐฐ์šฐ๊ธฐ',
    done: true
  },
  {
    id: 3,
    text: '๊ฐ์ฒด์™€ ๋ฐฐ์—ด ๋ฐฐ์šฐ๊ธฐ',
    done: true
  },
  {
    id: 4,
    text: '๋ฐฐ์—ด ๋‚ด์žฅํ•จ์ˆ˜ ๋ฐฐ์šฐ๊ธฐ',
    done: false
  }
];

const index = todos.findIndex(todo => todo.id === 3);
console.log(index); // ๊ฒฐ๊ณผ ๊ฐ’ 2

ย 

5) find

  • ์ฐพ์•„๋‚ธ ๊ฐ’ ์ž์ฒด๋ฅผ ๋ฐ˜ํ™˜
const todos = [
  {
    id: 1,
    text: '์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ž…๋ฌธ',
    done: true
  },
  {
    id: 2,
    text: 'ํ•จ์ˆ˜ ๋ฐฐ์šฐ๊ธฐ',
    done: true
  },
  {
    id: 3,
    text: '๊ฐ์ฒด์™€ ๋ฐฐ์—ด ๋ฐฐ์šฐ๊ธฐ',
    done: true
  },
  {
    id: 4,
    text: '๋ฐฐ์—ด ๋‚ด์žฅํ•จ์ˆ˜ ๋ฐฐ์šฐ๊ธฐ',
    done: false
  }
];

const todo = todos.find(todo => todo.id === 3);
console.log(todo);
  • ๊ฒฐ๊ณผ : {id: 3, text: "๊ฐ์ฒด์™€ ๋ฐฐ์—ด ๋ฐฐ์šฐ๊ธฐ", done: true}

ย 

6) filter

  • ๋ฐฐ์—ด์—์„œ ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฐ’๋“ค๋งŒ ๋”ฐ๋กœ ์ถ”์ถœํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ ๋‹ค.
  • todos ๋ฐฐ์—ด์—์„œ done ๊ฐ’์ด false ์ธ ํ•ญ๋ชฉ๋“ค๋งŒ ๋”ฐ๋กœ ์ถ”์ถœํ•ด์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ์ƒ์„ฑ
const todos = [
  {
    id: 1,
    text: '์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ž…๋ฌธ',
    done: true
  },
  {
    id: 2,
    text: 'ํ•จ์ˆ˜ ๋ฐฐ์šฐ๊ธฐ',
    done: true
  },
  {
    id: 3,
    text: '๊ฐ์ฒด์™€ ๋ฐฐ์—ด ๋ฐฐ์šฐ๊ธฐ',
    done: true
  },
  {
    id: 4,
    text: '๋ฐฐ์—ด ๋‚ด์žฅํ•จ์ˆ˜ ๋ฐฐ์šฐ๊ธฐ',
    done: false
  }
];

const tasksNotDone = todos.filter(todo => todo.done === false);
console.log(tasksNotDone);
  • ๊ฒฐ๊ณผ
[
  {
    id: 4,
    text: '๋ฐฐ์—ด ๋‚ด์žฅ ํ•จ์ˆ˜ ๋ฐฐ์šฐ๊ธฐ',
    done: false
  }
];

โฌ‡ ์•„๋ž˜์ฒ˜๋Ÿผ๋„ ์‚ฌ์šฉ๊ฐ€๋Šฅ

const tasksNotDone = todos.filter(todo => !todo.done);

ย 

7) splice

  • ๋ฐฐ์—ด์—์„œ ํŠน์ • ํ•ญ๋ชฉ์„ ์ œ๊ฑฐํ•  ๋•Œ ์‚ฌ์šฉ
  • splice ๋ฅผ ์‚ฌ์šฉ ํ•  ๋•Œ ์ฒซ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ์–ด๋–ค ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ์ง€์šธ์ง€๋ฅผ ์˜๋ฏธํ•˜๊ณ  ๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ๊ทธ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ๋ช‡๊ฐœ๋ฅผ ์ง€์šธ์ง€๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
const numbers = [10, 20, 30, 40];
const index = numbers.indexOf(30);
numbers.splice(index, 1);
console.log(numbers);
  • ๊ฒฐ๊ณผ : [10, 20, 40]
    ย 

8) slice

  • ๋ฐฐ์—ด์„ ์ž˜๋ผ๋‚ผ ๋•Œ ์‚ฌ์šฉ, ๊ธฐ์กด์˜ ๋ฐฐ์—ด์€ ๊ฑด๋“ค์ด์ง€ ์•Š๋Š”๋‹ค.
  • slice ์—๋Š” ๋‘๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„ฃ๊ฒŒ ๋˜๋Š”๋ฐ ์ฒซ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ์–ด๋””์„œ๋ถ€ํ„ฐ ์ž๋ฅผ์ง€, ๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ์–ด๋””๊นŒ์ง€ ์ž๋ฅผ์ง€๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
const numbers = [10, 20, 30, 40];
const sliced = numbers.slice(0, 2); // 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ 2์ „๊นŒ์ง€

console.log(sliced); // [10, 20]
console.log(numbers); // [10, 20, 30, 40]

ย 

9) shift ์™€ pop

  • shift : ์ฒซ๋ฒˆ์งธ ์›์†Œ๋ฅผ ๋ฐฐ์—ด์—์„œ ์ถ”์ถœํ•ด์ค€๋‹ค. (์ถ”์ถœํ•˜๋Š” ๊ณผ์ •์—์„œ ๋ฐฐ์—ด์—์„œ ํ•ด๋‹น ์›์†Œ๋Š” ์‚ฌ๋ผ์ง„๋‹ค.)
const numbers = [10, 20, 30, 40];
const value = numbers.shift();
console.log(value);
console.log(numbers);
  • ๊ฒฐ๊ณผ
10
[20, 30, 40]
  • pop : push์˜ ๋ฐ˜๋Œ€, push๋Š” ๋ฐฐ์—ด์˜ ๋งจ ๋งˆ์ง€๋ง‰์— ์ƒˆ ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€, pop์€ ๋งจ ๋งˆ์ง€๋ง‰ ํ•ญ๋ชฉ์„ ์ถ”์ถœ
const numbers = [10, 20, 30, 40];
const value = numbers.pop();
console.log(value);
console.log(numbers);
  • ๊ฒฐ๊ณผ
40
[10, 20, 30]

ย 

10) unshift

  • shift์˜ ๋ฐ˜๋Œ€, ๋ฐฐ์—ด์˜ ๋งจ ์•ž์— ์ƒˆ ์›์†Œ ์ถ”๊ฐ€
const numbers = [10, 20, 30, 40];
numbers.unshift(5);
console.log(numbers);
  • ๊ฒฐ๊ณผ : [5, 10, 20, 30, 40]
    ย 

11) concat

  • ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฐฐ์—ด์„ ํ•˜๋‚˜์˜ ๋ฐฐ์—ด๋กœ ํ•ฉ์ณ์คŒ
  • ๊ธฐ์กดํ•จ์ˆ˜์— ์˜ํ•ญ์„ ์ฃผ์ง€ ์•Š์Œ.
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const concated = arr1.concat(arr2);

console.log(concated);
  • ๊ฒฐ๊ณผ : [1, 2, 3, 4, 5, 6];
    ย 

12) join

  • ๋ฐฐ์—ด ์•ˆ์˜ ๊ฐ’๋“ค์„ ๋ฌธ์ž์—ด ํ˜•ํƒœ๋กœ ํ•ฉ์ณ์คŒ.
const array = [1, 2, 3, 4, 5];
console.log(array.join()); // 1,2,3,4,5
console.log(array.join(' ')); // 1 2 3 4 5
console.log(array.join(', ')); // 1, 2, 3, 4, 5

ย 

13) reduce

  • ์ฒซ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” accumulator ์™€ current ๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๊ฐ€์ ธ์™€์„œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ฝœ๋ฐฑํ•จ์ˆ˜, ๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” reduce ํ•จ์ˆ˜์—์„œ ์‚ฌ์šฉ ํ•  ์ดˆ๊นƒ๊ฐ’
  • ๋ฐฐ์—ด์˜ ์ด ํ•ฉ ๊ตฌํ•˜๊ธฐ
const numbers = [1, 2, 3, 4, 5];
let sum = numbers.reduce((accumulator, current) => accumulator + current, 0);

console.log(sum); // ๊ฒฐ๊ณผ ๊ฐ’ : 15
  • ๋ฐฐ์—ด์˜ ํ‰๊ท  ๊ตฌํ•˜๊ธฐ
const numbers = [1, 2, 3, 4, 5];
let sum = numbers.reduce((accumulator, current, index, array) => {
  if (index === array.length - 1) {
    return (accumulator + current) / array.length;
  }
  return accumulator + current;
}, 0);

console.log(sum); // ๊ฒฐ๊ณผ ๊ฐ’ : 3

ย 

14) ํ€ด์ฆˆ

  • ์ˆซ์ž ๋ฐฐ์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ 10๋ณด๋‹ค ํฐ ์ˆซ์ž์˜ ๊ฐฏ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ธฐ
function countBiggerThanTen(numbers) {
  let count = 0;
  for(let number of numbers){
    if(number>10) count++;
  }
  return count;
}

const count = countBiggerThanTen([1, 2, 3, 5, 10, 20, 30, 40, 50, 60]);
console.log(count); // 5

ย 

By Dozzing

๋‹ต๊ธ€ ๋‚จ๊ธฐ๊ธฐ

์ด๋ฉ”์ผ ์ฃผ์†Œ๋Š” ๊ณต๊ฐœ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•„์ˆ˜ ํ•„๋“œ๋Š” *๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค