0

So I am confused between all these four styles of declaring and defining a function ? all these work similarly but there must be a subtle difference between them :D

var sayHello = function() {
  console.log("hello")
  console.log(arugments)//works
  console.log(this)//works
}

function sayhello() {
  console.log("hello")
  console.log(arugments)//works
  console.log(this)//work
}

var SayHello = function f() {
  console.log("hello")
    console.log(arugments)//works
  console.log(this)//work
}

var SayHellO = () =>{  
   console.log(arugments)//doesnt works
  console.log(this)//works
}
John Mellow
  • 116
  • 5
  • Some functional differences you will notice are _var function (or better const function) has to be declared before used_ and _arrow functions do not have a "this" when used as eventListeners_ – mplungjan Dec 07 '21 at 07:50
  • Off-topic: get rid of the legacy `var` and use `let` and `const` – Andreas Dec 07 '21 at 07:52
  • @Andreas what's the possible difference are those gonna make ? let is good for block level scoping , a function at maximum exists in global context henceforth it can be accessed anywhere and const yeah it is reasonable to use but since it isn't gonna make it well documented since no one's gonna replace the function expression – John Mellow Dec 07 '21 at 07:55
  • @Andreas The root cause of using var seems to be [this](https://stackoverflow.com/questions/70186161/hoisting-does-not-seem-to-be-working-properly#comment124080384_70186161). – Teemu Dec 07 '21 at 07:55
  • @mplungjan but an arrow function expression has this ? – John Mellow Dec 07 '21 at 07:56
  • https://stackoverflow.com/questions/28798330/arrow-functions-and-this – mplungjan Dec 07 '21 at 08:01

0 Answers0