+ 6
JS Coder This is a good discussion.
I can relate to Schindlabua in that, sometime between 2012 and 2014, I might have replaced the practice using:
typeof obj === "undefined"
with:
obj === undefined
However, I typically reserve checking for undefined if I'm working with variables that weren't initialized with default values, which has been rare for me since ES6 has been heavily adopted by all modern browsers.
In recent years, ES6 features, Babel, JS Lint, TypeScript, JS module pattern usage, feature rich JS IDEs, libraries like ReactJS, unit tests, code reviews, and other tools / practices have made it easier and safer to use checks as simple as:
if(obj) {...}
However, this would not work well on legacy JS code or any code that pollutes the global scope.
+ 6
Schindlabua Maybe I should change my name to Gandalf. 😂🤣
JS Coder I take it back about you reminding me of my younger self.
You are much more impressive than I ever was at 15. 😉
+ 5
JS Coder That's because the `undefined` in the function scope is, well... as you pointed out, in a separate scope. 😉
Therefore, this function scope instance is different from the instance in the global scope, a.k.a. `window.undefined`.
You can test this with the following line:
console.log(window.undefined === undefined)
This will output true in global and false in the function after the line: `var undefined = 5`.
+ 4
Yes you are right, the only two values that are double-equals to null are null and undefined.
However your version is more confusing for sure, if you just have a quick over the code look it's not clear what you are doing or if you have missed something.
I think if you want to check for exactly those two values you should be explicit about it, I prefer `if(x !== undefined && x !== null)`.
+ 4
JS Coder Righteous, righteous 😎🤘!
It's awesome to see new professional developers joining the community to share their knowledge with the new generation of programmers. 🙏
+ 3
I am not a JavaScript programmer.
So I googled it.
https://www.google.nl/amp/s/www.ajaymatharu.com/javascript-difference-between-undefined-and-null/amp/
It seems to me that in JavaScript.
"Undefined" is the real null (according to me as c# programmer)
And null is a value that a programmer can use to initialize objects.
Please correct me if I am wrong
Do Google null vs undefined, you will be suprised.
+ 3
JS Coder The only possible settings I can imagine where you don't know whether a variable has been declared are:
- after using `eval`
- inside a `with` block
Usage of `with` is no longer recommended and usage of `eval` is extremely rare, checking for the existence of a variable afterwards is even rarer.
In all other cases I know that the variable has been declared because I have just written it's name into that if statement. (And if I made a typo I want an error to be thrown)
I think the `typeof` check solves a problem nobody is really having. Of course in the extremest of edge-cases you might need it, you are right. But you should use it then and only then.
(It certainly hasn't happened to me even once in 10+ years.)
+ 3
David Carroll Yep, `if(obj)` is clearly superior if applicable :P
+ 3
JS Coder I must be mixing my conversations up with other people. Either way... glad to have you in the community. I've enjoyed your focus on understanding code beyond the surface. You remind me of a younger me.
Oh man... I really hope you're younger than me. 😂🤣
+ 3
Seriously, looks like you have done a lot more than just your homework. Rock on, JS Coder!
+ 2
yes it is possible to do it this way
+ 2
Hah, fair. I'm not sure I like what ejs is doing there but I guess it is a usecase!
+ 2
rahul kumar I agree with everything you said except the part about being able to change the value of undefined.
Can you elaborate on this part?
+ 1
David Carroll The average person here is 15 and you are at least 10 times that so chances are good :D
EDIT: called it!
+ 1
JS Coder +- 50 years, yeah.
+ 1
The concrete difference between them is that null is explicit, while undefined is implicit.
In JavaScript, both null and undefined have a falsy value so when you do a equality check with == they are considered the same. If you use a strict equality check with === they are considered different.
Unassigned variables are initialized by JavaScript with a default value of undefined. JavaScript never sets a value to null. That must be done programmatically.
You can change the value of undefined by undefined=true; but you cannot change the value or null. This means that, null is object type where as undefined is undefined type.
While performing primitive operations, null is converted to zero (0) whereas undefined is converted to NaN.
http://net-informations.com/js/iq/nuvsun.htm