JS method to create a function of

1. function declaration

Individual function declarations in the main code stream.

function sayHi() {
  alert( "Hello" );


2. function expression

A function that creates or another grammatical structure in an expression. Here, the function assignment expression  = to create the right.

let sayHi = function() {
  alert( "Hello" );

Ever wonder why there is a function of the static type a semicolon at the end, and not a function declaration?

The answer is simple:

  • Function declaration is a code block, it is not required at the end  ;, as  if { ... }, for { }, function f { } and the like are not added back grammatical structure.
  • Function expression usually declare:  let sayHi = ...;, as a variable. It is not a block of code but for some assignments. Whatever the value, I would recommend using a semicolon at the end of the statement  ;. So here is the semicolon and function expressions does not have any relationship, it just terminates the statement.


3. Arrow function

let sum = (a, b) => a + b;


Guess you like

Origin www.cnblogs.com/sese/p/11685016.html