[Javascript] Null and Undefined in JavaScript

less than 1 minute read

In my dailyLife, usually I code server-side code, but sometime I am not front-end engineer, I encounter problem of jquery of raw javascript.

especially, I saw undefined error. usally I encountered this error, when API in server send the data, between name of property in class or entity from server and front are diffence. so solution is very easy, which making name of each side same.

in this time, I have question about undefined and like me, junior confuses null.

what is undefined ??

  1. primitive type, so undefined can be assigned
  2. in the condition, it is same with false
var input = undefined

if(!input){
    console.log('undefined???')
}
else{
    console.log('hi')
}

console print ‘undefined???’

what is null ??

  1. primitive, so it can be also assigned
  2. in the condition, it is same with false

funny thing, you can use like this

var thisIsTrue = undefined == null // true
var butThisIsFalse = undefied === null // false
  • we can guess that those are difference type.
typeof(null) // object
typeof(undefined) // undefined

conclusion

null doesn’t have value.
undefined doesn’t have type and value.

Leave a comment