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

const one = function() {
  console.log("function one");
// Outputs: "function 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 ""
Truong An

Truong An

Self-learner, passionate software engineer from Vietnam

Read More