0

Are Null, Undefined and false are same in data types in javascript ?

if(null){
    //This code won't work
 }

if(false){
   //This code won't work
}

if(undefined){
   //This code won't work
}

are they same theoretically ?

2
  • 3
    no, they are all different types (null, Boolean, undefined) Commented Jan 23, 2021 at 11:06
  • 1
    null is an Object. false is a Boolean. undefined is of type undefined. But they have in common the fact that they are all falsy values. Which means that when they are converted to Booleans (implicitely or explicitely), they translate to false, which is why none of your if conditions pass. The same would happen to 0, or an empty String "" Commented Jan 23, 2021 at 11:06

2 Answers 2

2
  • Data type of null is object
  • Data type of false is boolean
  • Data type of undefined is undefined

So they are different.

Sign up to request clarification or add additional context in comments.

Comments

1

Internally, JavaScript sets a value to one of six primitive data types:

  • Undefined (a variable with no defined value)
  • Null (a single null value)
  • Boolean (true or false)
  • Number (this includes Infinity and NaN – not a number!)
  • String (textual data)
  • Symbol (a unique and immutable primitive new to ES6/2015)
  • Everything else is an Object — including arrays.

Here are the complete list of JavaScript falsy values Complete list of JavaScript falsy values

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.