0
<script type="text/javascript">
  setTimeout(alert('1'), 1000);
  setTimeout(() => alert('a'), 1000); // second setTimeout method
</script>

1) what is the meaing of "() =>" in the second setTimeout() statement?

Sometimes I see the () before the fat arrow => means the input argument and the statement after fat arrow => means the implementation, but for the above second setTimeout() method, seem the fat arrow is not this meaning, as setTimeout() really need two input parameter (callback function and delay time in milliseconds),

2) and in the above second setTimeout method, the statement after => is the input parameter of setTimeout(), not the implementation....

user1169587
  • 1,104
  • 2
  • 17
  • 34

3 Answers3

3

Its syntactic sugar with also an different scope.

() => {}

Is equivalent to:

function(){

}

if you see an arrow function without {} that means the value right to it is gonna be returned:

() => someVal

Equivalent:

function(){
   return someVal;
}

In your case:

setTimeout(() => alert('a'), 1000);

The equivalent would be:

setTimeout(function(){
   return alert('a')
}, 1000);

Note: Arrow functions doesnt work on IE

bill.gates
  • 14,145
  • 3
  • 19
  • 47
0

You're defining a function, that is calling other function: alert

same as:

setTimeout(function() { return alert('a') }, 1000)
Tomasz Giba
  • 507
  • 1
  • 8
  • 22
-1

arrowFunction its a syntactic sugar for function(){....} so actually

() => {}

==

function() {
}
Omer Shacham
  • 618
  • 4
  • 11
  • 2
    no. it's not just syntactic sugar since it has different scoping. – GottZ May 28 '20 at 08:26
  • @GottZ the scoping is the same, the context isn't. – VLAZ May 28 '20 at 09:26
  • @VLAZ technically the scope is the surrounding lexical enclosing environment. https://tc39.es/ecma262/#sec-arrow-function-definitions-runtime-semantics-namedevaluation – GottZ May 28 '20 at 12:28