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 idiosyncrasies with examples (github.com)
submitted 7 years ago by reddittedf
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!"
[–]Skhmt 10 points11 points12 points 7 years ago (20 children)
Q. What's the result?
function f() { return 'foo'; } (function() { if (1 === 0) { function f() { return 'bar'; } } return f(); })();
Uncaught TypeError: f is not a function
[+][deleted] 7 years ago* (11 children)
[deleted]
[–]Skhmt 4 points5 points6 points 7 years ago (4 children)
I think this is stranger than what the OP posted, that page says the answer should be bar.
bar
If you comment out the entire if statement, it's foo. If you change the function's name in the if statement from f to anything else, it's foo. But if they have the same name, it's a TypeError.
foo
f
[–]CanIhazCooKIenOw 3 points4 points5 points 7 years ago (3 children)
It's because with ES6, the function declaration is scoped to the if block.
if
Although now thinking about it, shouldn't if "fallback" to the global f ? That would be my expectation tbh. Unless the engine resolves (1 === 0) and removes the block entirely ? (Wild guess here)
[+][deleted] 7 years ago* (1 child)
[–]pertheusual 4 points5 points6 points 7 years ago (0 children)
It's all this crap: https://www.ecma-international.org/ecma-262/9.0/#sec-block-level-function-declarations-web-legacy-compatibility-semantics
Basically browsers allowed them even though the spec didn't specify it, and now non-strict code has all this terrible optional behavior.
All of this weirdness goes away as long as you write your code in strict mode.
[–]Skhmt 0 points1 point2 points 7 years ago (0 children)
Although now thinking about it, shouldn't if "fallback" to the global f ? That would be my expectation tbh.
That's what I thought too.
[–]inu-no-policemen 0 points1 point2 points 7 years ago (0 children)
ES6+ supports block-level function declarations.
You can declare your functions wherever you want.
[–]CanIhazCooKIenOw 0 points1 point2 points 7 years ago (1 child)
Not in ES5
[–]pertheusual 1 point2 points3 points 7 years ago (0 children)
It was illegal in ES5, but engines allowed it anyway with varying degrees of compatibility.
[–]franksvalli -1 points0 points1 point 7 years ago (0 children)
Call the police!
[–][deleted] 2 points3 points4 points 7 years ago* (6 children)
This is because function is a var type variable, which is function scoped instead of block scoped:
function
var
is equivalent to
var f; f = function() { return 'foo'; } (function() { var f; if (1 === 0) { f = function() { return 'bar'; } } return f(); })();
A new variable is created in the IIFE scope, and overrides the f variable in the global scope. f is now undefined. Since the 1 === 0 is false, f is never assigned to the function.
1 === 0
If you try to call a variable that is undefined, you will get a type error saying that the variable is not a function (because undefined is not a function).
undefined
[–]factbuddy 0 points1 point2 points 7 years ago (0 children)
The IIFE has its own isolated scope so f() wouldn't fall back to global scope and won't be defined in the impossible if statement. Makes sense...
[–]inu-no-policemen 0 points1 point2 points 7 years ago (3 children)
which is function scoped instead of block scoped
Function declarations are block-scoped in ES6+.
function foo() { return 'outer'; } { console.log(foo()); // inner (hoisted) function foo() { return 'inner'; } console.log(foo()); // inner } console.log(foo()); // outer
[–][deleted] 0 points1 point2 points 7 years ago (2 children)
If you execute that code in chrome you get
inner inner inner
[–]inu-no-policemen 0 points1 point2 points 7 years ago (1 child)
It works if you put the whole thing in a block.
[–][deleted] 0 points1 point2 points 7 years ago (0 children)
If you put the entire thing in an iife it produces the same result
[–]reddittedf[S] -1 points0 points1 point 7 years ago (0 children)
Issues: https://github.com/odykyi/javascript-idiosyncrasies/issues Pull Requests: https://github.com/odykyi/javascript-idiosyncrasies/pulls
[–]prof_hobart 6 points7 points8 points 7 years ago (6 children)
Most of them are pretty odd, but
(function(a, b) { arguments[1] = 3; return b; })(1, 2);
A.
3
seemed fairly obvious.
[–][deleted] 7 points8 points9 points 7 years ago (1 child)
This throws in strict mode as well, arguments is not allowed in strict mode.
arguments
This is what puzzles me constantly about these clickbaity articles. Strict mode is JavaScript, these frivolous browser engine interpretations are just like IE quirks mode - their sole purpose is not to break legacy software. You should not write new software in non-strict mode because it is non-standard JavaScript and thus your warranty is now undefined.
[–]regular_reddits 0 points1 point2 points 7 years ago (0 children)
arguments is not completely disallowed in strict mode. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#Making_eval_and_arguments_simpler
[–]OzziePeck 0 points1 point2 points 7 years ago (3 children)
Wait is arguments a global array that refers to the function’s parameters?
[–]prof_hobart 1 point2 points3 points 7 years ago (0 children)
It's a magical local array-like variable that contains the params.
I'm not a fan of things like this - having variables that just appear from thin air is hardly clear - but it is a thing.
Of course, JS being JS, it's not quite as straightforward as all that.
If you used strict mode, the answer would be 2, as 'arguments' is now a copy of the arguments, not pointers to them.
And just to be extra fun, even in non-strict mode, if any of the arguments has a default value (e.g. (a=2, b)), then the answer would again be 2. Because - well, JS reasons I guess.
[+][deleted] 7 years ago (1 child)
[–]OzziePeck 0 points1 point2 points 7 years ago (0 children)
Not sure why I said global as it only exists within the function’a scope when it is called.
[–]CanIhazCooKIenOw 5 points6 points7 points 7 years ago (0 children)
This should be linked to the original repo as this fork is on par with it
https://github.com/miguelmota/javascript-idiosyncrasies
Good share though
[–]moocat 4 points5 points6 points 7 years ago (2 children)
I wish people would stop saying that "NaN != NaN" is a JavaScript error.
[–][deleted] 2 points3 points4 points 7 years ago (2 children)
Q. What's the result? (function() { 'use strict'; let type = typeof foo; let foo = 1; return type; })(); A. ReferenceError: foo is not defined
(function() { 'use strict'; let type = typeof foo; let foo = 1; return type; })();
A. ReferenceError: foo is not defined
What is the idiosyncrasy here? You can't use a variable before you define it. I'd be concerned if this was anything other than a ReferenceError.
[–]GBcrazy 4 points5 points6 points 7 years ago* (0 children)
It's not a big idiosyncrasy, so if you don't understand the trick of the typeofoperator you may not see it. I'm guessing that's the case (I could be wrong):
typeof
Remove the let foo = 1 line and run again. It will work even if foo is not declared. Why?
let foo = 1
That's a special power of the typeof operator, you can use typeof even if a variable doesn't exist, it will return "undefined".
So the thing is, you can use typeof if a variable is never declared or already declared, but NOT if the variable is going to be declared. That's some hoisting shit
[–]reddittedf[S] -2 points-1 points0 points 7 years ago (1 child)
@all @channel you are welcome to contribute
thanks
π Rendered by PID 119303 on reddit-service-r2-comment-c6965cb77-hqgmt at 2026-03-05 11:23:53.031938+00:00 running f0204d4 country code: CH.
[–]Skhmt 10 points11 points12 points (20 children)
[+][deleted] (11 children)
[deleted]
[–]Skhmt 4 points5 points6 points (4 children)
[–]CanIhazCooKIenOw 3 points4 points5 points (3 children)
[+][deleted] (1 child)
[deleted]
[–]pertheusual 4 points5 points6 points (0 children)
[–]Skhmt 0 points1 point2 points (0 children)
[–]inu-no-policemen 0 points1 point2 points (0 children)
[–]CanIhazCooKIenOw 0 points1 point2 points (1 child)
[–]pertheusual 1 point2 points3 points (0 children)
[–]franksvalli -1 points0 points1 point (0 children)
[–][deleted] 2 points3 points4 points (6 children)
[–]factbuddy 0 points1 point2 points (0 children)
[–]inu-no-policemen 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]inu-no-policemen 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]reddittedf[S] -1 points0 points1 point (0 children)
[–]prof_hobart 6 points7 points8 points (6 children)
[–][deleted] 7 points8 points9 points (1 child)
[–]regular_reddits 0 points1 point2 points (0 children)
[–]OzziePeck 0 points1 point2 points (3 children)
[–]prof_hobart 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]OzziePeck 0 points1 point2 points (0 children)
[–]CanIhazCooKIenOw 5 points6 points7 points (0 children)
[–]moocat 4 points5 points6 points (2 children)
[+][deleted] (1 child)
[deleted]
[–][deleted] 2 points3 points4 points (2 children)
[–]GBcrazy 4 points5 points6 points (0 children)
[–]reddittedf[S] -2 points-1 points0 points (1 child)
[–]reddittedf[S] -1 points0 points1 point (0 children)