Functions can be overriden by declaring them more than once in JavaScript

2020-01-18

Example:

function f() {
  return 1
}

function f() {
  return 2
}

f()

// Result: 2

Declaring functions by declaring const instead prevents this by raising an error:

const f = () => 1

const f = () => 2

// Uncaught SyntaxError: Identifier 'f' has already been declared

or

const f = () => 1

function f() {
  return 2
}

// Uncaught SyntaxError: Identifier 'f' has already been declared

From discussion on StackOverflow: https://stackoverflow.com/questions/33040703/proper-use-of-const-for-defining-functions-in-javascript#comment84466206_42803030