8

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?

apsillers
  • 107,868
  • 16
  • 221
  • 236
Henri Cavalcante
  • 435
  • 4
  • 16
  • 3
    Why would you expect `null` when the `undefined` value exists? – Quentin Mar 24 '16 at 13:25
  • null is assigned manually. – kris Mar 24 '16 at 13:27
  • IDK, I'm just asking, in PHP we have null as default return. – Henri Cavalcante Mar 24 '16 at 13:28
  • 1
    Possible duplicate of [What does javascript function return in the absence of a return statement?](http://stackoverflow.com/questions/1557754/what-does-javascript-function-return-in-the-absence-of-a-return-statement) – But I'm Not A Wrapper Class Mar 24 '16 at 13:28
  • It's not duplicated, im asking why undefined instead of null – Henri Cavalcante Mar 24 '16 at 13:29
  • You need to ask the person who designed the language, not Stack Overflow. – Felix Kling Mar 24 '16 at 13:33
  • I did a clarifying edited to possibly avoid closure; if you don't like it, feel free to roll back or edit more. – apsillers Mar 24 '16 at 13:43
  • @FelixKling I did it, waiting for a answer. https://twitter.com/henricavalcante/status/712991622372298753 – Henri Cavalcante Mar 24 '16 at 14:05
  • @CyberneticTwerkGuruOrc A closer duplicate might be [What is the difference between null and undefined in JavaScript?](http://stackoverflow.com/questions/5076944/what-is-the-difference-between-null-and-undefined-in-javascript) This question asks about a specific consequence of that difference, but it's one that might be obvious once the difference between `null` and `undefined` is well understood. – apsillers Mar 24 '16 at 14:14

7 Answers7

8

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
T.J. Crowder
  • 959,406
  • 173
  • 1,780
  • 1,769
apsillers
  • 107,868
  • 16
  • 221
  • 236
3

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

Steve G.
  • 590
  • 8
  • 15
  • 3
    @HenriCavalcante — Why would you expect something that the code *doesn't define* to be `null` instead of `undefined`? – Quentin Mar 24 '16 at 13:28
  • 1
    Its just the intended behavior. That's just the way the language was designed. Undefined and Null are two separate types in the javascript language. – Steve G. Mar 24 '16 at 13:29
2

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.

Nikhil Aggarwal
  • 27,657
  • 4
  • 40
  • 56
1

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

Michał Miszczyszyn
  • 10,641
  • 2
  • 34
  • 53
1

Brendan Eich, creator of JavaScript, answered my tweet.

@BrendanEich Why JavaScript function returns `undefined` by default instead of `null`? Is it just an specification or has a reason for that?

-- Henri Cavalcante on Twitter

@henricavalcante null means no object, undefined means no value, wider type. Are you asking why both null & undefined in JS?

-- Brendan Eich on Twitter

Answer by Brendan Eich

T.J. Crowder
  • 959,406
  • 173
  • 1,780
  • 1,769
Henri Cavalcante
  • 435
  • 4
  • 16
0

null is a defined value, there are two states for any variable, defined and undefined.

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

Every function in javascript has a implicit return: return; like in any other language to mark the end of the function. So it returns with nothing, which if you try to log, it will give you undefined

Gopikrishna S
  • 2,000
  • 3
  • 21
  • 35
-1

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.