filmov
tv
What are Null and Undefined in Javascript?

Показать описание
Although Null literally means nothing, when it comes to Javascript it is actually something, well kind of.
If you create a variable called x, you can assign it null which means x now has a value but that value is nothing.
Undefined on the other hand means that you’ve declared a variable but not assigned it anything.
So, why and when do you use them?
Well, there’s not actually much difference between them but the best use case would be perhaps if you were returning data from a function.
You need to return something from the function, you need to tell the caller that it’s complete, but that something being returned is nothing. Much easier to pass back null than an undefined variable.
#javascript #undefined #shorts
If you create a variable called x, you can assign it null which means x now has a value but that value is nothing.
Undefined on the other hand means that you’ve declared a variable but not assigned it anything.
So, why and when do you use them?
Well, there’s not actually much difference between them but the best use case would be perhaps if you were returning data from a function.
You need to return something from the function, you need to tell the caller that it’s complete, but that something being returned is nothing. Much easier to pass back null than an undefined variable.
#javascript #undefined #shorts