๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ’ฌ/ใ…ใ……ใ…Œใ…‹ใ…ใ…… ์ฑŒ๋ฆฐ์ง€

20์ผ์ฐจ

๐Ÿ‘ฉ‍๐Ÿš€

20์ผ์ฐจ

 

Part 4. JavaScript Level up

Ch 1. JS ๋ฐ์ดํ„ฐ

 


 

Ch 1. JS ๋ฐ์ดํ„ฐ

 

 

์ฃผ์ œ

 

  • JS ๋ฌธ๋ฒ•

 

 

์ฃผ์š” ๋‚ด์šฉ

 

 ๐Ÿ”— ๋ฌธ์ž 

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String

 

String - JavaScript | MDN

String ์ „์—ญ ๊ฐ์ฒด๋Š” ๋ฌธ์ž์—ด(๋ฌธ์ž์˜ ๋‚˜์—ด)์˜ ์ƒ์„ฑ์ž์ž…๋‹ˆ๋‹ค.

developer.mozilla.org

  • string.prototype.indexOf(str): ์ฃผ์–ด์ง„ ๊ฐ’ str๊ณผ ์ผ์น˜ํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜
  • string.prototype.slice(startIndex, endIndex): ์ธ๋ฑ์Šค startIndex๋ถ€ํ„ฐ endIndex-1 ๊นŒ์ง€์˜ ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜
  • string.prototype.replace(str1, str2): ๋ฌธ์ž์—ด str1์„ str2๋กœ ๋Œ€์ฒด
  • string.prototype.match(): ์ •๊ทœํ‘œํ˜„์‹
  • string.prototype.trim(): ๊ณต๋ฐฑ ์ œ๊ฑฐ

 

 ๐Ÿ”— ์ˆซ์ž์™€ ์ˆ˜ํ•™ 

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math

 

Math - JavaScript | MDN

Math๋Š” ์ˆ˜ํ•™์ ์ธ ์ƒ์ˆ˜์™€ ํ•จ์ˆ˜๋ฅผ ์œ„ํ•œ ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง„ ๋‚ด์žฅ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.

developer.mozilla.org

  • parseInt() / parseFloat()

  • Math.abs(x): ์ ˆ๋Œ“๊ฐ’ ๋ฐ˜ํ™˜
  • Math.min(x, y): x, y ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’ ๋ฐ˜ํ™˜
  • Math.max(x, y): x, y ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฐ’ ๋ฐ˜ํ™˜
  • Math.ceil(x): ์˜ฌ๋ฆผ 
  • Math.floor(x): ๋‚ด๋ฆผ
  • Math.round(x): ๋ฐ˜์˜ฌ๋ฆผ
  • Math.random(): ๋žœ๋ค ์ˆซ์ž ๋ฐ˜ํ™˜

 

 ๐Ÿ”— ๋ฐฐ์—ด 

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array

 

Array - JavaScript | MDN

JavaScript Array ํด๋ž˜์Šค๋Š” ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ์˜ ๊ณ ์ˆ˜์ค€ ๊ฐ์ฒด์ธ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์ „์—ญ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.

developer.mozilla.org

 

  • Array.prototype.find(callback): ์ฃผ์–ด์ง„ ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์š”์†Œ์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜
  • Array.prototype.concat(arr): ๋ฐฐ์—ด ํ•ฉ์น˜๊ธฐ (์›๋ณธ ์œ ์ง€)
const numbers = [1, 2, 3, 4];
const fruits = ["Apple", "Banana", "Cherry"];

console.log(numbers.concat(fruits)); // ์›๋ณธ ์œ ์ง€!
console.log(numbers);
console.log(fruits);

 

 

  • Array.prototype.forEach(callback(item, index)): ๋ฐฐ์—ด ๋ฐ์ดํ„ฐ์˜ ์•„์ดํ…œ์˜ ๊ฐœ์ˆ˜ ๋งŒํผ ํŠน์ •ํ•œ callback ํ•จ์ˆ˜๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ์‹คํ–‰ => ๋”ฐ๋กœ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ’์€ ์—†๋‹ค.
const numbers = [1, 2, 3, 4];
const fruits = ["Apple", "Banana", "Cherry"];

fruits.forEach(function (element, index, array) {
  console.log(element, index, array);
});

 

 

  • Arrow.prototype.map(callback(item, index)): callback ๋‚ด๋ถ€์—์„œ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ์•„ ๋†“์€ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ ๋ฐ˜ํ™˜ (์›๋ณธ ์œ ์ง€)
const numbers = [1, 2, 3, 4];
const fruits = ["Apple", "Banana", "Cherry"];

const a = fruits.forEach((fruit, index) => {
  console.log(`${fruit}-${index}`);
});
console.log(a);

const b = fruits.map((fruit, index) => ({
  id: index,
  name: fruit,
}));
console.log(b);

 

 

  • Array.prototype.filter(callback(item, index)): ๋ฐฐ์—ด ๋ฐ์ดํ„ฐ ์•ˆ์— ๋“ค์–ด ์žˆ๋Š” ๊ฐ๊ฐ์˜ ์•„์ดํ…œ๋“ค์„ ํŠน์ •ํ•œ ๊ธฐ์ค€์— ์˜ํ•ด์„œ ํ•„ํ„ฐ๋ง, ํ•„ํ„ฐ๋œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๋ฐ์ดํ„ฐ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ (์›๋ณธ ์œ ์ง€)
const numbers = [1, 2, 3, 4];
const fruits = ["Apple", "Banana", "Cherry"];

const a = numbers.map((number) => number < 3);
console.log(a);

const b = numbers.filter((number) => number < 3);
console.log(b);

console.log(numbers);

 

 

  • Array.prototype.find(callback): callback ๋‚ด์—์„œ ๋ญ”๊ฐ€๋ฅผ ์ฐพ์œผ๋ฉด ๋ฐ˜๋ณต์ด ์ข…๋ฃŒ๋˜๊ณ , ์ฐพ์•„์ง„ ๋ฐฐ์—ด์˜ ์•„์ดํ…œ์ด ๋ฐ˜ํ™˜
  • Array.prototype.findIndex(callback): ์œ„์˜ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ ๋ฐ˜ํ™˜
const numbers = [1, 2, 3, 4];
const fruits = ["Apple", "Banana", "Cherry"];

const a = fruits.find((fruit) => /^B/.test(fruit));
console.log(a);

const b = fruits.findIndex((fruit) => /^B/.test(fruit));
console.log(b);

 

 

  • Array.prototype.includes(item): ๋ฐฐ์—ด์— item์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ์—ฌ๋ถ€ ๋ฐ˜ํ™˜
const numbers = [1, 2, 3, 4];
const fruits = ["Apple", "Banana", "Cherry"];

const a = numbers.includes(3);
console.log(a);

const b = fruits.includes("HEROPY");
console.log(b);

 

 

  • Array.prototype.push(item): ๋ฐฐ์—ด ๊ฐ€์žฅ ๋’ค์— item ์‚ฝ์ž… (์›๋ณธ ์ˆ˜์ •๋จ ์ฃผ์˜)
  • Array.prototype.unshift(item): ๋ฐฐ์—ด ๊ฐ€์žฅ ์•ž์— item ์‚ฝ์ž… (์›๋ณธ ์ˆ˜์ •๋จ ์ฃผ์˜)
const numbers = [1, 2, 3, 4];
const fruits = ["Apple", "Banana", "Cherry"];

numbers.push(5);
console.log(numbers);

numbers.unshift(0);
console.log(numbers);

 

 

  • Array.prototype.reverse(): ๋ฐฐ์—ด ์•„์ดํ…œ ์ˆœ์„œ ๋’ค์ง‘๊ธฐ (์›๋ณธ ์ˆ˜์ •๋จ ์ฃผ์˜)
const numbers = [1, 2, 3, 4];
const fruits = ["Apple", "Banana", "Cherry"];

numbers.reverse();
fruits.reverse();

console.log(numbers);
console.log(fruits);

 

 

  • Array.prototype.splice(index, deleteCount, item1, item2 ...): index๋ถ€ํ„ฐ deleteCount ๋งŒํผ ์•„์ดํ…œ ์‚ญ์ œ & ๊ทธ ์ž๋ฆฌ์— item1 ๋ผ์›Œ๋„ฃ๊ธฐ (์›๋ณธ ์ˆ˜์ •๋จ ์ฃผ์˜)
const numbers = [1, 2, 3, 4];
const fruits = ["Apple", "Banana", "Cherry"];

numbers.splice(2, 1, 99);
console.log(numbers);

fruits.splice(2, 0, "Orange");
console.log(fruits);

 

 

 

๋Š๋‚€์ 

 

์ •๋ฆฌ ํž˜๋“ค๋‹ค................