-1
console.log(addB(10, 15));

function addB(a, b) {
  return a + b;
}

console.log(addC(10, 15));

const addC = (a, b) => {
  return a + b;
};

Just convert function to arrow function. why get "Cannot access 'addC' before initialization" error .. affter that function line I call it its working. what is the limitation to use arrow function

Ravindr
  • 186
  • 3
  • 15
  • 1
    Function declarations are hoisted. Assignments (of any kind of value) to variables are not. – Quentin Jul 07 '22 at 12:50
  • 1
    arrow function are anonymous function associated to a variable, this means you can't access it before initialization juste like a variable – nem0z Jul 07 '22 at 12:51
  • See: https://stackoverflow.com/questions/3887408/javascript-function-declaration-and-evaluation-order/3887590#3887590 – slebetman Jul 07 '22 at 13:08

2 Answers2

1

Arrow functions are not hoisted, so you cannot call them before you declare them.

Gaurav Kandpal
  • 1,250
  • 2
  • 15
  • 33
1

Look at this code:

console.log(addC(10, 15));

const addC = (a, b) => {
  return a + b;
};

You call addC and pass it as a parameter to console.log and then define addC. Therefore, when you console.log(addC(10, 15));, addC is not defined yet. You can move the call to console.log after the definition to solve the problem.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175