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

Why do JavaScript functions return undefinedby default instead null? Is this a completely arbitrary choice by specification or is there a broader ECMAScript behavior context in which this particular choice can be understood?

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

What is the difference between nulland undefined? Is there a spec-based reason why it is undefinedmore suitable as a default return value, or was it an arbitrary choice?

+4
source share
6 answers

null undefined:

undefined

, ,

null

, -

undefined . . null . null , , .

, , undefined ,

  • , ,
+3

JavaScript void, . - undefined, , .

undefined, null - : undefined - (undefined), null.

, , , undefined.

+1

ECMAScript , undefined, . .

. [[Call]] :

9.2.1 [[Call]] (thisArgument, argumentsList)

[[Call]] ECMAScript F thisArgument argumentsList, ECMAScript. :

...

11. NormalCompletion (undefined).

, null undefined - JavaScript. , :

4.3.10 undefined

, ,

4.3.12

, -

0

null - , , undefined, .

var a; // declared but undefined, has nothing in it; does not mean its null
var b = null; // declared and defined as null;

Each function in javascript has an implicit return: return;like in any other language, to mark the end of a function. Thus, it returns without anything, that if you try to log in, it will give youundefined

0
source

All Articles