Skip to main content

Command Palette

Search for a command to run...

Demistifying null and undefined in javascript ?

Updated
1 min read
S

I love learning about technology and sharing that with others

Hope you will like this, please share your thoughts in the discussion so that we can build on this.

What's the difference between undefined and null?

Answer. Similarities between null and undefined.

  • In javascript 7 both comes under the primitive type
let primitiveTypes[]= {'string','number','boolean', 'null' , 'undefined' , 'symbol' , 'bigint' }
  • when we print both undefined and null they will give "undefined"
console.log(null)

null

console.log(undefined)

undefined
  • Lets use ==, === to compare both

console.log(null==undefined)

true

console.log(null === undefined ) 

false
  • Undefined is basically assigned when there is the only declaration for the function,variable, class
variable :
let onlyDeclarared

console.log(onlyDeclarared)

output -

undefined

#####function :

const withoutBodyFunc = () => {}
console.log(withoutBodyFunc())


output 

undefined
Object
const someObj = {
  a: 'test',
  b: 'btest',
  c: 'ctest',
}

console.log(someObj['d'])

output 

undefined.

More from this blog

H

hashcodehub

271 posts

Consistent, Passionate and Organized :)