0

I've being seeing that the Arrow functions are the ES6 version of (normal) Javascript functions. Is it correct ? Or are they the ES6 version of anonymous functions ?

// Normal Function
function oldOne() {
 console.log("Hello World..!");
}

//Anonymous function
var anonym = function(){
 console.log("Hello World..!");
}

// ES6
var arrowFun = () => {
 console.log("Hello World..!");
}

In above code snippet, if we run each function before it's declaration, only normal function would give the output( Hello World..! ) right ? So, if arrow functions are the new version of normal function it also should give the same output (instead of giving an error, like anonymous functions do).

So I just wanted to clarify whether they're ES6 version of Normal Functions or Anonymous Functions ?

Thidasa Pankaja
  • 930
  • 8
  • 25
  • 44
  • 4
    That thing about a "normal" function being accessible before its declaration is called hoisting. You can read about it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function – Nisarg Shah Mar 22 '18 at 11:14
  • 1
    arrow functions are anonymous functions without scope. – Jonas Wilms Mar 22 '18 at 11:16
  • 1
    Basically the main difference is the `this` and arguments. https://stackoverflow.com/questions/34361379/arrow-function-vs-function-declaration-expressions-are-they-equivalent-exch – Marco Talento Mar 22 '18 at 11:16
  • 1
    For reference, the proper terminology is "function statement" (in place of "normal function") and "function expression" (in place of anonymous function). Function expressions *can* be anonymous, but often aren't, and there's nothing abnormal about them! The essential answer is that arrow functions are a kind of function expression, but there are many important differences. – lonesomeday Mar 22 '18 at 11:16
  • I got it now. @lonesomeday Isn't it **Function declaration**? `Function declarations in JavaScript are hoisted to the top of the enclosing function or global scope` - [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function) – Thidasa Pankaja Mar 22 '18 at 11:25
  • @ThidasaParanavitharana You're right: I did indeed mean "function declaration". Statement isn't wrong *per se*, but you're right that it isn't the precise terminolgy – lonesomeday Mar 22 '18 at 11:30

0 Answers0