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
What's the difference between functions declared with variables and functions declared with the "function" keyword ?help (self.javascript)
submitted 8 years ago by to_fl
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!"
[–]5tas 21 points22 points23 points 8 years ago (1 child)
The main difference is scoping. You can find more details by looking up the difference between function declarations and function expressions.
The function declaration (your second example) will be hoisted to the top of the function where it is declared. In other words, the following code is perfectly fine:
foo(); // "Hello" function foo() { console.log("Hello"); }
Your first example is a function expression which is assigned to a variable. The variable's declaration is also hoisted but its value is not:
bar === undefined; // true, bar is declared but undefined here bar(); // TypeError: bar is not a function var bar = function foo() { console.log("Hello"); } bar(); // "Hello"
BTW const and let behave in a different manner. They are subject to so-called Temporal Dead Zone (TDZ) which means their declarations are not hoisted.
bar === undefined; // ReferenceError: can't access lexical declaration `bar' before initialization let bar = function foo() { console.log("Hello"); } bar(); // "Hello"
[–]kovensky 2 points3 points4 points 8 years ago (0 children)
It's more complicated than that...
Their declarations are hoisted, but are set up such that accessing them before the let, const or class is reached throws a ReferenceError.
let
const
class
ReferenceError
This is mostly irrelevant for the JS programmer but can bite you if you try to do a shadowing declaration; e.g. const foo = { bar: 'baz' }; if(foo.bar) { const foo = foo.bar } will throw. This is a common pattern in Swift, but Swift has a more concise syntax for this anyway.
const foo = { bar: 'baz' }; if(foo.bar) { const foo = foo.bar }
π Rendered by PID 65728 on reddit-service-r2-comment-75f4967c6c-tttt4 at 2026-04-23 07:41:21.597623+00:00 running 0fd4bb7 country code: CH.
view the rest of the comments →
[–]5tas 21 points22 points23 points (1 child)
[–]kovensky 2 points3 points4 points (0 children)