This is an episode of the podcast 'I Know How this Works' that can be found on iTunes. This episode is describes how to null, undefined, and undeclared are different and similar.
Closed Captioned:
Hello and welcome to another episode of 'I Know How this Works'. Today we're going to discuss JavaScript: null, undefined, and undeclared. What they are, and what the differences are, and how to tell the difference in your code.
But before we get started, I wanted to say hello and thank you to my sponsors: AwesomeWebStore.com where you can find the lastest news on technology and tools and tips on how to run and manage your online business. Lots of business automation.
ThatOneTee.com where you can find the latest and freshest graphic tees
out there and where you can buy them.
And 5-10.club where you can buy clothing at near wholesale prices
in multiples of 5 and 10 in a variety of colors.
Now null, undefined and undeclared. I know how this works and I am awefulBrown. Each one of these has a very unique purpose and you can use these to write your code null is when you actively assign no value to a variable. var a declares the variable and then you give it a value of null
which is no value. var k; declares a variable, but you see there's no value assigned. that is undefined, you're passively giving no value to the variable. And then b assigned 2, you don't declare it. You just assign the value. So that is null, undefined, and undeclared. The way you can tell the
difference between the two, especially null and undefined; if you do an equivalency test to see if they have the same value you will get true Both null and undefined have a value of undefined. But if you look at the type
You do a strict equivalency test, you'll see that null is not undefined because they are different types. If you do a typeof test for a variable that's assigned null you will get object. The typeof null is object.
Where undefined, the type of undefined is going to be undefined.
And with undeclared, the typeof will give you the typeof will give
you the value that it is. For instance, b assigned 2 will give you number.
c assigned the string "asdf" will give you string. Every undeclared variable is a global property. It is a global variable. These other two will basically be where they're assigned and where they're declared. Right so, if it's declared inside a function it will be a local variable. For both of these. A real world example, a good example I got from watching the video Java Brains, and I'll put a link to that and some other information inside the description, if you're filling out a job application, and on the application it says what is your middle name, or there is a box for your middle name, and you don't have a middle name and you put n/a, that is null. You're actively saying there is no value for this. Where as, if you just skip it and you don't see it or you just ignore it you're not actively giving it no value. You're passively giving it no value. That would be undefined. So there you go. That is null, undefined, and undeclared. And I Know How this Works so that you will too. Thank you for the sponsors:
AwesomeWebStore.com, ThatOneTee.com, and 5-10.club.
And please subscribe, share, and comment. Let me know how I'm
doing. What it is you'd like to know so that I can show you I Know How this Works and then you can too. Until the next episode, I'm awefulBrown.
Sources:
Java Brains
undefined, null and the difference playlist
• JavaScript for Developers 12 - Understandi...
null
https://developer.mozilla.org/en-US/d...
undefined
https://developer.mozilla.org/en-US/d...
undeclared
https://developer.mozilla.org/en-US/d...
Sponsors:
AwesomeWebStore.com
@AweWS
ThatOneTee.com
@AweWS
5-10.Club
@5-10Club