CodeZettaInterview Hub

JavaScript · Hoisting · Senior · Best Practice

In non-strict sloppy mode, what happens if two function declarations share the same name in one scope? How should a team prevent related bugs?

Short Interview Answer

Later function declarations overwrite earlier ones during instantiation; enforce modules/strict mode and lint rules to ban duplicates.

Detailed Explanation

In a single function or global sloppy script, duplicate `function` declarations are allowed; the last one wins when declarations are instantiated. That can silently change behavior if two helpers share a name after a merge. Strict mode and ES modules still disallow some duplicate binding forms and make errors louder. At team level: use ESLint `no-redeclare` / `no-func-assign`, prefer `const` function expressions or exports with unique names, and keep one source of truth per symbol. Block-level function declaration rules also vary historically across browsers — another reason to avoid relying on them.

Example

function handle() { return 'first'; }
function handle() { return 'second'; }
handle(); // 'second'

Interview Tip

Pivot to engineering process: linting and modules as prevention, not just language trivia.

Common Mistake

Assuming duplicate function declarations always throw SyntaxError in every environment.

Did you know this answer?