iPod Video White

๐Ÿ““ TIL

[220421] ์—ฐ์‚ฐ์ž/if /switch

k_m_jin 2022. 4. 22. 13:55

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

์—ฐ์‚ฐ์ž ์ด๋ฆ„
== ๋™๋“ฑ
!= ๋ถ€๋“ฑ
=== ์ผ์น˜
!== ๋ถˆ์ผ์น˜
a>b  
a>=b  
a<b  
a<=b  

์ดํ€„ ๊ธฐํ˜ธ๋Š” ํ•ญ์ƒ ๋’ค์—

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

  • && ๊ทธ๋ฆฌ๊ณ  : ๊ฐ€์žฅ ๋จผ์ € ์ฐพ์€ falsy ๋ฐ˜ํ™˜ ์—†๋‹ค๋ฉด ๋งˆ์ง€๋ง‰์„ ๋ฐ˜ํ™˜
    const divEl = document.querySelector('div') //null
    divEl && divEl.addEventListener('~')
  • || ๋˜๋Š” : ๊ฐ€์žฅ ๋จผ์ € ์ฐพ์€ truthy ๋ฐ˜ํ™˜ ์—†๋‹ค๋ฉด ๋งˆ์ง€๋ง‰์„ ๋ฐ˜ํ™˜
  • ! ๋ถ€์ • : truthy๋ฉด false๋กœ, falsy๋ฉด true๋กœ ๋ฐ”๋€œ

์‚ผํ•ญ ์—ฐ์‚ฐ์ž

์กฐ๊ฑด ! ์ฐธ์ด๋ฉด ํ˜ธ์ถœ : ๊ฑฐ์ง“ ์ด๋ฉด ํ˜ธ์ถœ

a
  ? a===1 ? hello() : null
  :nill

์ค‘์ฒฉ์„ ๊ถŒ์žฅํ•˜์ง€ ์•Š์Œ

if statement

if (truthy) {

} else if (์กฐ๊ฑด2) {

} else {

}

switch statement

์กฐ๊ฑด์ด ํŠน์ •ํ•œ ๊ฐ’ ์ผ๋•Œ ์‚ฌ์šฉ
if>switch

switch ( ์กฐ๊ฑด){
  case ํŠน์ •๊ฐ’:
  case ํŠน์ •๊ฐ’: //์ค‘์ฒฉ๊ฐ€๋Šฅ
    console.log()
    break //๋งˆ์ง€๋ง‰๊ณผ default ๋Š” ํ•„์š”์—†์Œ
  default:
}

for statement

for (์‹œ์ž‘์กฐ๊ฑด; ์ข…๋ฃŒ์กฐ๊ฑด; ๋ณ€ํ™”์กฐ๊ฑด) {

}
for ( let i =0; i<10; i +=1 ){

}
const ulEl = document.querySelector('ul')
for (let i =0; i<10; i +=1 ){
  const liEl = document.createElement('li')
  liEl.textContent = i
  ulEl.appendChild(liEl)
}
๋ฐ˜์‘ํ˜•