Skip to main content

Command Palette

Search for a command to run...

Difference between Undefined and Null

Published
1 min readView as Markdown
Difference between Undefined and Null
N

Hi there, My name is Nirav. I am from India. Currently I am learning Java and Dsa. I can code in Html, Css, Javascript, Python ⭐ My projects:

  1. https://pro-quiz-app.herokuapp.com
  2. https://guessnumberofficial.netlify.app ✨ GitHub: https://github.com/Niravprajapati1/

Hey folks, Today i will tell you the difference between Undefined and Null.

What is Undefined?

Suppose you have to go to picnic and you want to carry a laptop but now you carry 2 laptops. Why you carry 2 laptops? because if you any one laptop not work or battery is low. So therefore you can use your second laptop In short if you want to have a backup that backup is your Undefined

In terms of coding we use undefined to declare a variable. Like a variable that doesn't have any value.

let a;
console.log(a);

You will get undefined as Output.

let b;
// assign a value to variable 
b = "Hey";
//Now if you do console.log(b) you will got "Hey" as Output
 console.log(b);

Now what is Null?

Null is like nothing. In JavaScript, null is treated as an object. You can check this using the typeof operator. The typeof operator determines the type of variables and values. For example,

const nothing = null;
console.log(typeof(nothing));

Conclusion

In javascript undefined and Null both have boolean value of false

let a = undefined;
let b = null;
console.log(boolean(a));
console.log(boolean(b));

So you can use null and undefined for some testing and also for backup.

More from this blog

Nirav Prajapati

7 posts

I love to contribute in open source always ready for learning.