Frontend/JavaScript

[ μžλ°”μŠ€ν¬λ¦½νŠΈ(JavaScript) ] typeof μ—°μ‚°μžμ— λŒ€ν•΄ μ•Œμ•„λ³΄μž

YWTechIT 2021. 11. 3. 20:09
728x90

πŸ“ typeof μ—°μ‚°μžμ— λŒ€ν•΄ μ•Œμ•„λ³΄μž

❏ typeof μ—°μ‚°μž

  1. typeof μ—°μ‚°μžλŠ” ν”Όμ—°μ‚°μžμ˜ 데이터 νƒ€μž…μ„ λ¬Έμžμ—΄λ‘œ λ°˜ν™˜ν•œλ‹€. 총 7가지 λ¬Έμžμ—΄ string, number, boolean, undefined, symbol, object, function 쀑 ν•œ 개λ₯Ό λ°˜ν™˜ν•œλ‹€. null을 λ°˜ν™˜ν•˜λŠ” κ²½μš°λŠ” μ—†μœΌλ©°, ν•¨μˆ˜μ˜ 경우 function을 λ°˜ν™˜ν•œλ‹€.
  2. typeof(null)의 결괏값은 null이 μ•„λ‹ˆλΌ objectλ₯Ό λ°˜ν™˜ν•˜λŠ”λ° μ£Όμ˜ν•˜μž. 이것은 μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ 첫 번째 λ²„μ „μ˜ 버그닀.(typeof null의 역사) ν•˜μ§€λ§Œ κΈ°μ‘΄ μ½”λ“œμ— 영ν–₯을 쀄 수 있기 λ•Œλ¬Έμ— μ•„μ§κΉŒμ§€ μˆ˜μ •λ˜μ§€ λͺ»ν•˜κ³  μžˆλ‹€. λ§Œμ•½, null νƒ€μž…μΈμ§€ ν™•μΈν•˜κ³  μ‹Άλ‹€λ©΄ 일치 μ—°μ‚°μž(===)λ₯Ό μ‚¬μš©ν•˜μ—¬ ν™•μΈν•˜μž.
  3. μ„ μ–Έν•˜μ§€ μ•Šμ€ μ‹λ³„μžλ₯Ό typeof μ—°μ‚°μžλ‘œ 연산해보면 ReferenceErrorκ°€ λ°œμƒν•˜μ§€ μ•Šκ³  undefinedλ₯Ό λ°˜ν™˜ν•œλ‹€.
728x90
typeof ('') // string
typeof (1) // number
typeof (NaN) // number
typeof (true) // boolean
typeof (undefined) // undefined
typeof (Symbol()) // symbol
typeof (null) // object
typeof ([]) // object
typeof ({}) // object
typeof (new Date()) // object
typeof (/test/gi) // object
typeof (function(){}) // function

let a = null
a === null // true

typeof(nothing) // undefined

reference

1. λͺ¨λ˜ μžλ°”μŠ€ν¬λ¦½νŠΈ λ”₯ λ‹€μ΄λΈŒ

λ°˜μ‘ν˜•