Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 658
Henri Cavalcante Avatar asked Mar 24 '16 13:03

Henri Cavalcante


4 Answers

The specification says of null and undefined:

undefined value

primitive value used when a variable has not been assigned a value

null value

primitive value that represents the intentional absence of any object value

undefined represents a failure to assign a value. It is the total absence of a value. null represents the positive assertion of a non-value in an object context. null is intended to be used when an object is expected but the current value is no-object.

Given these two definitions, it seems obvious that undefined is the correct choice, since

  1. functions can return values other than objects, and
  2. a failure to specify a return value maps neatly onto a failure to assign a value
like image 147
apsillers Avatar answered Sep 28 '22 08:09

apsillers


That's part of the specification. If no explicit return value is returned from a given function, the return value will always be undefined.

When a return statement is called in a function, the execution of this function is stopped. If specified, a given value is returned to the function caller. If the expression is omitted, undefined is returned instead

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return

like image 39
Steve G. Avatar answered Sep 28 '22 09:09

Steve G.


Few key statements to answer your question

JavaScript does not have a void type, so every function must return a value. The default value is undefined, except for constructors, where the default return value is this.

undefined and null are two distinct types: undefined is a type itself (undefined) while null is defined.

So, if you have returned nothing then it has to be nothing i.e. undefined.

like image 27
Nikhil Aggarwal Avatar answered Sep 28 '22 10:09

Nikhil Aggarwal


The ECMAScript specification explicitly states that functions return undefined if no other return is specified. It's the default behaviour of the language.

See the last step in the [[Call]] internal method specification:

9.2.1 [[Call]] ( thisArgument, argumentsList)

The [[Call]] internal method for an ECMAScript function object F is called with parameters thisArgument and argumentsList, a List of ECMAScript language values. The following steps are taken:

11. Return NormalCompletion(undefined).

Note that null and undefined are two distinct values in JavaScript. Again, according the the specification:

4.3.10 undefined value

primitive value used when a variable has not been assigned a value

4.3.12 null value

primitive value that represents the intentional absence of any object value

like image 37
Michał Miszczyszyn Avatar answered Sep 28 '22 10:09

Michał Miszczyszyn