3

I know in JavaScript only function declarations are hoisted, which means it should print 30 after running the function sum.

However it says diff is not defined, shouldn't it be hoisted?

sum(10, 20);
diff(10, 20);
    
function sum(x, y) {
  return x + y;
}
    
let diff = function(x, y) {
  return x - y;
} 

2
  • because you call the function before you have defined it Commented Feb 3, 2021 at 18:54
  • To understand this, you need to understand the JavaScript hoisting mechanism. Basically, hoisting rewrites the code before executing, such that sum and diff are declared at the top, then sum is defined, your calls are made, then diff is defined (that definition is left after the calls because you used let). So you're calling before you define. Commented Feb 3, 2021 at 18:56

5 Answers 5

1

It's because as you said, only function declarations are hoisted, not function expressions (assigning a nameless function to a variable). Following code works:

sum(10, 20);
diff(10, 20);
    
function sum(x, y) {
  return x + y;
}
    
function diff(x, y) {
  return x - y;
} 

To declare diff the way you did, you must lift it to the top of your code:

let diff = function(x, y) {
  return x - y;
} 

sum(10, 20);
diff(10, 20);
    
function sum(x, y) {
  return x + y;
}
    

Sign up to request clarification or add additional context in comments.

Comments

0

Yes, let and const are hoisted but you cannot access them before the actual declaration is evaluated at runtime.

1 Comment

let and const declarations are hoisted, but their definitions are left in place. Same basic idea as what you're saying, but I wanted to be more precise.
0

You are accessing the function before you initialize it.

So this should work,

let diff = function(x, y) {
  return x - y;
} 

function sum(x, y) {
  return x + y;
}

console.log(sum(10, 20));
console.log(diff(10, 20));

Comments

0

let diff will hoist but at the moment when function diff(10, 20); will be called variable diff will not jet be defined with the function. Google this topic function declaration vs function expression

Comments

0

Because Function Expressions are not hoisted like Function Declarations

Function expressions in JavaScript are not hoisted, unlike function declarations. You can't use function expressions before you create them:

console.log(notHoisted) // undefined
//  even though the variable name is hoisted, the definition isn't. so it's undefined.
notHoisted(); // TypeError: notHoisted is not a function

var notHoisted = function() {
   console.log('bar');
};

https://developer.mozilla.org/en-US/docs/web/JavaScript/Reference/Operators/function#function_expression_hoisting


You will need to use a Function Declaration if you want it to be hoisted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.