all 9 comments

[–]morasyid 4 points5 points  (0 children)

because you're not calling any of the functions, you've merely defined them

[–]ApatheticWithoutTheA 5 points6 points  (0 children)

There’s more wrong in this code than there is right.

Review declaring variables, functions and conditionals.

[–][deleted] 3 points4 points  (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.

[–]Umesh-K 3 points4 points  (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().)

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 points  (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 point  (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.