undefined

<aside> ๐Ÿ’ก ๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š์„ ๋•Œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์ด ์ž๋™์œผ๋กœ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ๊ฐ’์ด๋‹ค.

</aside>

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ƒํ™ฉ์—์„œ undefined๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์ฆ‰, ์‚ฌ์šฉ์ž๊ฐ€ ๊ฐ’์„ ์ง€์ •ํ•ด ์ค„ ๊ฒƒ์ด๋ผ๊ณ  ์˜ˆ์ƒํ•˜์˜€์ง€๋งŒ ๊ทธ๋ ‡๊ฒŒ ๋˜์ง€ ์•Š์•˜์„ ๋•Œ ์ž๋™์œผ๋กœ undefined๋ฅผ ์ค€๋‹ค.

์‹ค์ œ๋กœ๋Š” undefined๋ฅผ ์ž๋™์œผ๋กœ ํ• ๋‹นํ•ด์ค€๋‹ค๊ณ  ์ƒ๊ฐํ•˜๊ธฐ ์‰ฝ์ง€๋งŒ ์‚ฌ์‹ค์€ ์•„๋ฌด๊ฒƒ๋„ ํ• ๋‹นํ•˜์ง€ ์•Š๊ณ 

var a
console.log(a) // undefined

var obj = { a: 1 }
console.log(obj.a) // 1
console.log(obj.b) // undefined
console.log(b) // ReferenceError: b is not defined

var func = function() {}
var c = func()
console.log(c) // undefined

undefined์™€ ๋ฐฐ์—ด

๊ฐ’์„ ๋Œ€์ž…ํ•˜์ง€ ์•Š์€ ๋ฐฐ์—ด์˜ ๊ฒฝ์šฐ์—๋Š” ์กฐ๊ธˆ ํŠน์ดํ•˜๋‹ค.

var arr1 = []
arr1.length = 3
console.log(arr1) // [ <3 empty items> ]

var arr2 = new Array(3)
console.log(arr2) // [ <3 empty items> ]

var arr3 = [undefined, undefined, undefined]
console.log(arr3) // [undefined, undefined, undefined]

<aside> ๐Ÿ’ก ๋นˆ ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ๋Š˜๋ฆฌ๊ฑฐ๋‚˜ new Array()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์—ˆ์„ ์‹œ์—๋Š” undefined๋„ ํ• ๋‹น๋˜์ง€ ์•Š๋Š”๋‹ค. ์ฆ‰, ๋น„์–ด ์žˆ๋Š” ์š”์†Œ์™€ undefined๋ฅผ ํ• ๋‹นํ•œ ์š”์†Œ๋Š” ์•„์˜ˆ ๋‹ค๋ฅด๋‹ค.

</aside>

var arr1 = [undefined, 1]
var arr2 = []
arr2[1] = 1 // [empty, 1]
console.log(arr2[0]) // undefined

arr1.forEach(function(data, index) {console.log(data, index)}) 
// undefined 0 / 1 1
arr2.forEach(function(data, index) {console.log(data, index)}) 
// 1 1

โ€˜๋น„์–ด ์žˆ๋Š” ์š”์†Œโ€™์˜ ๊ฒฝ์šฐ ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ๋“ค์˜ ์ˆœํšŒ ๋Œ€์ƒ์—์„œ ์ œ์™ธ๋œ๋‹ค. ๋‹น์—ฐํžˆ ์•„๋ฌด๊ฒƒ๋„ ์—†๋Š” ์š”์†Œ๋Š” ์ˆœํšŒํ•  ์ˆ˜ ์—†์œผ๋‹ˆ๊นŒ.