+ 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.
25th Jun 2019, 9:42 PM
David Carroll
David Carroll - avatar
+ 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. 😉
25th Jun 2019, 10:43 PM
David Carroll
David Carroll - avatar
+ 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`.
10th Jul 2019, 6:00 AM
David Carroll
David Carroll - avatar
+ 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)`.
24th Jun 2019, 1:50 AM
Schindlabua
Schindlabua - avatar
+ 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. 🙏
25th Jun 2019, 10:02 PM
David Carroll
David Carroll - avatar
+ 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.
23rd Jun 2019, 6:23 PM
sneeze
sneeze - avatar
+ 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.)
24th Jun 2019, 2:18 AM
Schindlabua
Schindlabua - avatar
+ 3
David Carroll Yep, `if(obj)` is clearly superior if applicable :P
25th Jun 2019, 10:07 PM
Schindlabua
Schindlabua - avatar
+ 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. 😂🤣
25th Jun 2019, 10:31 PM
David Carroll
David Carroll - avatar
+ 3
Seriously, looks like you have done a lot more than just your homework. Rock on, JS Coder!
25th Jun 2019, 10:54 PM
Schindlabua
Schindlabua - avatar
+ 2
yes it is possible to do it this way
23rd Jun 2019, 4:47 PM
Nico Ruder
Nico Ruder - avatar
+ 2
Hah, fair. I'm not sure I like what ejs is doing there but I guess it is a usecase!
24th Jun 2019, 2:37 AM
Schindlabua
Schindlabua - avatar
+ 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?
10th Jul 2019, 5:22 AM
David Carroll
David Carroll - avatar
+ 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!
25th Jun 2019, 10:39 PM
Schindlabua
Schindlabua - avatar
+ 1
JS Coder +- 50 years, yeah.
25th Jun 2019, 10:42 PM
Schindlabua
Schindlabua - avatar
+ 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
9th Jul 2019, 4:47 AM
rahul kumar
rahul kumar - avatar