If you not familiar with difference between null (something is defined but doesn't have assign any valueno value) and undefined ( something was not defined) then look to my post below:
What is a difference between null and undefined
During learning you discover how amazing thing can be done with if statement... and how useful it is.
Then during your practice your skill on examples, you will discover is good to check if something doesn't have a value ... so you just write:
if (typeof example === null) {
alert("booo!example doesn't have any value!");
}
Job done ... but you want check is something exists... you heard about undefined... but as you know undefined is not a reserved word ,so you can't do the same away as above... what to do ? You actually can do it in similar way you need to "compare" using string "undefined".
Let's see a example for check object property exist.
if (typeof example === "undefined") {
alert("example is undefined");
}
It will be very useful for you. in your deep relationship with javascript.
DOMINIK SYMONOWICZ's blogs - FOREVER HUNGRY of knowledge, sarcasm, smiling, music, dancing, and food ...lots of food. This IT blog is a collection of notes with a simple solution with an explanation, added documentation, sarcastic comment and weird example. I also write about a lesson learnt from my mistakes. A non-it blog can be found here: Dominik Symonowicz's non-IT blog and website
Showing posts with label undefined. Show all posts
Showing posts with label undefined. Show all posts
29 September 2011
11 March 2011
What is differenece between null and undefined ?
When you start learning how to develop software , then you learned that null means lack of value (and associated annoying error related to null called NullPointerException which cause people to madness and frenzy).
When you start learning javascript you discover that javascript is like other languages with some freaks,so i usually said that javascript is a language created by Monty Python.
Anyway.. back to topic, so ....
null means lack of value and when you see null it can indicate that something does not contain a valid string, number, boolean , array or ... object.
However Javascript loves conversion (that cause lots of bugs and problems when you develop your application and this will be main reason why you will hate this language in many cases but loves by some geeks in some rare cases).
Why ?
Keep this in mind , specially when you will do lots of comparisons.
For example compare null to undefined ,why ??
Let's explain what undefined means:
Undefined appear when
In string context to null
In number context you will see NaN (which means Not A Number) (why ? this is another story which will explain in one of future posts)
In boolean context to false
Few interesting things on end of this post:
Remeber:
In book : Javascript: The deinitive Guide i found that " Unlike null, undefined is not a reserved word in JavaScript."
And last .... people who are very sensitive in speaking/writing correctly always get mad because they said that "do not say/write null value .. say/write null ... why ? because null means lack of value)
So if you have no f... idea why result of your comparison is weird ambiguous unexpected or not logical .. look to this post again. It can save your nervous system!
When you start learning javascript you discover that javascript is like other languages with some freaks,so i usually said that javascript is a language created by Monty Python.
Anyway.. back to topic, so ....
null means lack of value and when you see null it can indicate that something does not contain a valid string, number, boolean , array or ... object.
However Javascript loves conversion (that cause lots of bugs and problems when you develop your application and this will be main reason why you will hate this language in many cases but loves by some geeks in some rare cases).
Why ?
Javascript will convert :
In a string context to null
In a number context to 0
In boolean context to false
For example compare null to undefined ,why ??
Let's explain what undefined means:
Undefined appear when
- Something wasn't declared and you try to call this. (like Object)
- Variable has been declared but never had a value assigned
- property of Object which you refer to doesn't exist.
In string context to null
In number context you will see NaN (which means Not A Number) (why ? this is another story which will explain in one of future posts)
In boolean context to false
Few interesting things on end of this post:
Remeber:
- null is related as lack of variable of something that exist
- undefined means that something doesn't exist.
In book : Javascript: The deinitive Guide i found that " Unlike null, undefined is not a reserved word in JavaScript."
And last .... people who are very sensitive in speaking/writing correctly always get mad because they said that "do not say/write null value .. say/write null ... why ? because null means lack of value)
So if you have no f... idea why result of your comparison is weird ambiguous unexpected or not logical .. look to this post again. It can save your nervous system!
Subscribe to:
Posts (Atom)