#stackoverlog#javascript

Difference between functionName = function() {} and function functionName() {}

We have:

const one = function () {
  console.log('function one')
}

function two() {
  console.log('function two')
}

The difference is that one is a function expression and so only defined when that line is reached, whereas two is a function declaration and is defined as soon as its surrounding function or script is executed (due to hoisting).

// TypeError: one is not a function
one();

const one = function() {
  console.log("function one");
};
// Outputs: "function two"
two();

function two() {
  console.log("function two");
}

Another difference is that the function name is assign automatically.

function abc(){};
console.log(abc.name); // prints "abc"
const abc = function(){};
console.log(abc.name); // prints ""
An Tran

An Tran

A passionate web developer, self-learner and music lover.

Read More