Frontend/Javascript

Undefined / null in Javascript

petitCoding 2022. 10. 13. 17:32

Javascript has a special type named undefined.

This means, someone defined the variable but didn't set the value yet.

For example: 

var a;

console.log(a); // undefined

 

Do you think null == undefined is true?

 

If you use == for comparing them, it's true. (false == false)

but if you use === , then it says false. (undefined !== null)

That means, they're basically different.

 

Here's an example for understanding this:

 

var obj = {
	a: null,
	b: 3
}


console.log("obj.a:", obj.a) 
console.log("obj.b:", obj.b) 
console.log("obj.c:", obj.c)



========== result ============

> "obj.a:" null
> "obj.b:" 3
> "obj.c:" undefined

 

 

반응형