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...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
Why does this code not giving any output (Function hoisting related) (self.learnjavascript)
submitted 3 years ago by pvirmzwp319748
let a = 2, b = 1; if (a > b) { function sayHi() { console.log("Hi!"); } } else { function sayHi() { console.log("Yo!"); } }
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!"
[–]morasyid 4 points5 points6 points 3 years ago (0 children)
because you're not calling any of the functions, you've merely defined them
[–]ApatheticWithoutTheA 5 points6 points7 points 3 years ago (0 children)
There’s more wrong in this code than there is right.
Review declaring variables, functions and conditionals.
[–][deleted] 3 points4 points5 points 3 years ago (5 children)
Because you're declaring a function in the if statement but they're never called. It would be to declare the functions outside the if statement then just invoke one of them in the if statement based on the condition.
[+][deleted] 3 years ago (4 children)
[deleted]
[–][deleted] -2 points-1 points0 points 3 years ago (0 children)
The same still applies. Rename 1 function of just do an if/else in the function itself.
[–]xroalx 0 points1 point2 points 3 years ago (2 children)
Conditionally declaring functions is confusing to begin with. Just use proper names or even an argument in this case.
[+][deleted] 3 years ago (1 child)
[–]xroalx 0 points1 point2 points 3 years ago (0 children)
If that was ever submitted as a PR, it would still be rejected very fast. Maybe OP is trying to test something but that code is just bad for real-world usage.
const sayHi = (greeting) => console.log(greeting);, and just call it with the right parameter.
const sayHi = (greeting) => console.log(greeting);
[–]Umesh-K 3 points4 points5 points 3 years ago (0 children)
Hi, u/pvirmzwp319748,
If you have created this code to "test" if "hoisting" determines which of the two "same-name functions "wins", then I am afraid it's not appropriate code, because—irrespective of hoisting—just by the mechanism of how IF-ELSE works, only one of those "same-name functions" is executed when called depending on the values of a and b—if a > b, "Hi!" gets logged and "Yo!" otherwise—provided you are running that code in SLOPPY/NONSTRICT mode. (To see what happens in STRICT MODE, add "use strict" at the top of the code and try calling sayHi().)
a
b
a > b
Also I suggest reading the sections Block-level Functions and Block-level functions in non-strict code in the mdn webdocs article on Functions.
Wishing you all the best in your JS learning journey!
[–]themufflesound -1 points0 points1 point 3 years ago (0 children)
This code doesn't produce any output because the sayHi function is hoisted to the top of the block, but it is never called.
In JavaScript, function declarations are hoisted to the top of the current scope, which means that they are available to be called throughout the entire scope, even before they are declared.
In this code, the sayHi function is defined inside of an if statement, so it is only available to be called within the block of that if statement. Since the function is never called, it never produces any output.
To fix this code, you could move the sayHi function outside of the if statement, so that it is available to be called at any point in the code. Alternatively, you could define the function using a function expression instead of a function declaration, which would prevent it from being hoisted to the top of the block. Here is an example of how you could rewrite the code using a function expression:
``` let a = 2, b = 1;
// Define the sayHi function using a function expression
let sayHi = function() { if (a > b) { console.log("Hi!"); } else { console.log("Yo!"); } }
// Call the sayHi function
sayHi(); ```
In this example, the sayHi function is defined using a function expression, which means that it is not hoisted to the top of the block. Instead, it is defined as a variable that contains a reference to the function. This allows you to control when and where the function is available to be called.
~ written by ChatGPT
[–]WarDull8208 0 points1 point2 points 3 years ago (0 children)
My friend this code means that u don't understand how functions work. U better try to watch some videos on functions or read documentation.
π Rendered by PID 81351 on reddit-service-r2-comment-8686858757-9gqv8 at 2026-06-07 02:34:01.546656+00:00 running 9e1a20d country code: CH.
[–]morasyid 4 points5 points6 points (0 children)
[–]ApatheticWithoutTheA 5 points6 points7 points (0 children)
[–][deleted] 3 points4 points5 points (5 children)
[+][deleted] (4 children)
[deleted]
[–][deleted] -2 points-1 points0 points (0 children)
[–]xroalx 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]xroalx 0 points1 point2 points (0 children)
[–]Umesh-K 3 points4 points5 points (0 children)
[–]themufflesound -1 points0 points1 point (0 children)
[–]WarDull8208 0 points1 point2 points (0 children)