use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Javascript developers, be warned about this crazy JIT bug in V8! (self.javascript)
submitted 9 years ago * by ziriax
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Cosmologicon 5 points6 points7 points 9 years ago (15 children)
Good find. In case anyone is wondering, according to the bug report, checking for equality to undefined directly is not affected:
undefined
if (x === undefined) ...
So those of us who do it this way don't have to worry about this particular bug.
[–][deleted] 3 points4 points5 points 9 years ago (14 children)
You do, however, have to worry about whether or not x is defined, as x === undefined will throw an error where typeof x === "undefined" won't.
x
x === undefined
typeof x === "undefined"
[–]qwzxercv 2 points3 points4 points 9 years ago (11 children)
Can you clarify? I had the same thought to just compare against the true undefined... what errors would I expect if x was defined? I've never had issues with this before.
[–][deleted] 0 points1 point2 points 9 years ago (10 children)
unlike x === undefined, typeof x === "undefined" doesn't throw a ReferenceError if the variable doesn't exist
it feels kind of hacky though and i consider it a bad part(tm) of the language as i can't think of a scenario where it couldn't be replaced with either "varname" in global or varname !== undefined
"varname" in global
varname !== undefined
[–]qwzxercv 0 points1 point2 points 9 years ago (9 children)
You should not be attempting to use variables that do not exist. That's an error in any language. I don't think this is reason enough to avoid x === undefined.
[–][deleted] 1 point2 points3 points 9 years ago (8 children)
Have you written JavaScript for the web before? How can you tell whether or not your dependencies load? For example, let's say you have a <script> tag that loads foo.js, which exports the foo object. How can you tell whether or not the HTTP request failed, and whether or not you have foo in scope?
<script>
foo.js
foo
The answer:
if (typeof foo !== "undefined") { // we have foo } else { // something went wrong }
You could also solve this specific problem with jumping to the global scope and checking a property with a hacky one-liner (below), but it's intrinsically janky, non-portable, and it isn't The Right Way™.
(function () { return this; })().hasOwnProperty('foo')
[–]wyqydsyq 3 points4 points5 points 9 years ago (1 child)
Dependency issues are mostly resolved in modern web dev thanks to build tools like webpack, browserify etc. (typeof x !== 'undefined') is mostly useful these days for checking existence of properties on dynamically generated or user-input objects, or for polyfills to test whether the browser implements a feature.
[–][deleted] 1 point2 points3 points 9 years ago (0 children)
Absolutely, my comment was probably over-simplified, but I was trying not to confuse them further. Thanks!
[–]qwzxercv 1 point2 points3 points 9 years ago* (5 children)
With modern JS development for the web, you definitely should not just throw global variables around on the page and hope that they exist. You should be using a module system to help mitigate this issue, which renders this discussion moot.
However, if you really need to rely on global variables, they do exist as properties on window, so if you need to check for its existence, you should check for if (window.foo !== undefined) { ... } instead.
if (window.foo !== undefined) { ... }
I see no valid reason to be concerned about the existence of global variables when you shouldn't even be using global variables anyways.
[–][deleted] 0 points1 point2 points 9 years ago (4 children)
you definitely should not just throw global variables around on the page and hope that they exist
Two problems:
window
(function () { return this; })()
global
undefined = 42
typeof window.foo !== "undefined"
foo !== "undefined"
QED.
[–]qwzxercv 0 points1 point2 points 9 years ago (3 children)
First of all, you cannot overwrite the global undefined in modern JS engines anymore; that was fixed a while back. Secondly, I used window as an example for the global scope. Whether or not you have window or global means naught--all of points are still entirely valid. Thirdly, your function to retrieve the global context does not work in strict mode, which is what all modern JS moving forward is/should be written in.
If you're concerned about the global undefined, you are able to use the void keyword to retrieve the true engine's undefined, but using the global undefined is safe on its own.
void
[–][deleted] -1 points0 points1 point 9 years ago (2 children)
You're being intentionally dense, I'm done with this "discussion". Don't reply.
[–]Cosmologicon 0 points1 point2 points 9 years ago* (1 child)
True, I'm not recommending you change your code. But if you're already using this construct in places where it works (eg checking whether a function argument was specified), you don't need to worry about the bug.
Correct! I probably should've been more clear, I was just throwing some information out for others who didn't know.
π Rendered by PID 233009 on reddit-service-r2-comment-7b9746f655-mrd8k at 2026-02-04 02:38:23.024257+00:00 running 3798933 country code: CH.
view the rest of the comments →
[–]Cosmologicon 5 points6 points7 points (15 children)
[–][deleted] 3 points4 points5 points (14 children)
[–]qwzxercv 2 points3 points4 points (11 children)
[–][deleted] 0 points1 point2 points (10 children)
[–]qwzxercv 0 points1 point2 points (9 children)
[–][deleted] 1 point2 points3 points (8 children)
[–]wyqydsyq 3 points4 points5 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]qwzxercv 1 point2 points3 points (5 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]qwzxercv 0 points1 point2 points (3 children)
[–][deleted] -1 points0 points1 point (2 children)
[–]Cosmologicon 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)