Why do functions return `undefined` instead of `null` by default?

Viewed 2182

Why do JavaScript functions return undefined by default instead of null? Is this a totally arbitrary choice by the specification, or is there a larger ECMAScript-behavior context in which this particular choice can be understood?

function a() {}
a();
// undefined

What is the difference between null and undefined? Is there a specification-based reason why is undefined more appropriate as a default return value, or was it an arbitrary choice?

7 Answers

If the fact that function comes as undefined is causing problems, you can use classes instead. They can be accessed the exact same way, but it will count as an object.

Related