[TIL] more about JS

Sooยท2022๋…„ 11์›” 27์ผ
0
post-custom-banner

โ“ for ๋ฌธ์ด ์žˆ๋Š”๋ฐ for ... in for...of ๋ฌธ๋ฒ•์ด ์ƒˆ๋กœ ๋‚˜์˜จ ์ด์œ ๋Š” ๋ฌด์—‡์ผ๊นŒ์š”?

๊ทธ๋ฆฌ๊ณ  ์šฐ๋ฆฌ๊ฐ€ ์ด๊ฒƒ์„ ๊ณ ๋ฅด๋Š” ๊ธฐ์ค€์€ ๋ฌด์—‡์ผ๊นŒ์š”?

๐Ÿ”ย ย for ๋ฌธ์ด ์žˆ๋Š”๋ฐ for ... in for...of ๋ฌธ๋ฒ•์ด ์ƒˆ๋กœ ๋‚˜์˜จ ์ด์œ ๋Š” ๋ฌด์—‡์ผ๊นŒ์š”?

  • forโ€ฆin ๋ฌธ : ๊ฐ์ฒด๋ฅผ ์ข€ ๋” ์‰ฝ๊ฒŒ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•ด ์ƒ๊ฒจ๋‚œ ์ œ์–ด๋ฌธ์ด๋‹ค.
  • forโ€ฆof๋ฌธ : ES6์—์„œ ์ƒˆ๋กœ ์ƒ๊ธด ๋ฐ˜๋ณต๋ฌธ์ด๋ฉฐ ์ปฌ๋ ‰์…˜์˜ ์š”์†Œ์— ๋ฃจํ”„๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด๋‹ค.

ย for in ๋ฐ˜๋ณต๋ฌธ

  • ๋ฐฐ์—ด๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ˜๋ณต๋ฌธ์ด๋ฉฐ, ๋ฐฐ์—ด ์š”์†Œ ํ•˜๋‚˜ํ•˜๋‚˜๋ฅผ ๊บผ๋‚ด์„œ ํŠน์ • ๋ฌธ์žฅ์„ ์‹คํ–‰ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
  • ๋ฐ˜๋ณต ๋ณ€์ˆ˜์— ์ธ๋ฑ์Šค๊ฐ€ ๋“ค์–ด๊ฐ€๊ณ , ๋ฐ˜๋ณต๋ฌธ ๋‚ด๋ถ€์— ์š”์†Œ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด fruits[i]์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
for (const ๋ฐ˜๋ณต ๋ณ€์ˆ˜ in ๋ฐฐ์—ด ๋˜๋Š” ๊ฐ์ฒด) {
๋ฌธ์žฅ
}

ย for of ๋ฐ˜๋ณต๋ฌธ

  • for of ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉฐ ์•ˆ์ •์„ฑ์„ ์œ„ํ•ด ๋ช‡ ๊ฐ€์ง€ ์ฝ”๋“œ๋ฅผ ๋” ์ถ”๊ฐ€ํ•˜๊ธฐ๋กœ ํ•˜์˜€๋Š”๋ฐ, ๊ทธ๋ž˜์„œ ๋“ฑ์žฅ ํ•œ ๊ฒƒ์ด for of ๋ฐ˜๋ณต๋ฌธ์ด๋‹ค.
  • ์š”์†Œ์˜ ๊ฐ’์„ ๋ฐ˜๋ณตํ•  ๋•Œ ์•ˆ์ •์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
for (const ๋ฐ˜๋ณต ๋ณ€์ˆ˜ of ๋ฐฐ์—ด ๋˜๋Š” ๊ฐ์ฒด) { // for in ๋ฐ˜๋ณต๋ฌธ๊ณผ ๋‹ฌ๋ฆฌ ๋ฐ˜๋ณต ๋ณ€์ˆ˜์— ์š”์†Œ๊ฐ’์ด ๋“ค์–ด๊ฐ
๋ฌธ์žฅ
}

ย for ๋ฐ˜๋ณต๋ฌธ

  • ์ผ๋ฐ˜์ ์œผ๋กœ ํŠน์ • ํšŸ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฒ”์šฉ์ ์ธ ๋ฐ˜๋ณต๋ฌธ์ด๋‹ค.
profile
Soogineer's Devlog
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€