7

I was writing some code in JavaScript. When I accidentally came across this.

undefined = 'some value' //does not give any error

true = 'some value';   //gives error

null = 'some value';   //gives error

How is it that first statement is valid whereas the other two are invalid. From what I know both undefined, true, and null are values you can assign to some variable, so all these should be invalid statements.

3
  • how is it that first statement is valid whereas the other two are invalid that's what the spec says. You should use strict mode, it makes a bit more sense (can't assign to undefined). Commented Jul 6, 2017 at 7:38
  • note that even if it does give no error, undefined's value will stay undefined. Which is not a case inside a function (with the keyword var, or else it will be no different from first case) Commented Jul 6, 2017 at 7:40
  • @alex I just wanted to know why the assignment works is all. Commented Jul 6, 2017 at 7:43

4 Answers 4

10

From MDN:

undefined is a property of the global object; i.e., it is a variable in global scope. The initial value of undefined is the primitive value undefined.

Hence, you can assign the value to undefined unlike true and null which are reserved keywords. Note that this is the same case with NaN as well which is again not a reserved keyword and hence, you can assign any value to it.


Just to add more to this, it doesn't matter even if you are assigning a value to undefined, it will not write to it as it is a readonly property.

Quoting from MDN again.

In modern browsers (JavaScript 1.8.5 / Firefox 4+), undefined is a non-configurable, non-writable property per the ECMAScript 5 specification. Even when this is not the case, avoid overriding it.

enter image description here


Prefer using strict-mode in your JavaScript by declaring "use strict" at the very top of the file or inside a function to avoid such things. Using something like

"use strict";
undefined = 'test'; //will raise an error, refer to [1]

[1] VM1082:2 Uncaught TypeError: Cannot assign to read only property 'undefined' of object '#'

Sign up to request clarification or add additional context in comments.

1 Comment

When I tested your last case in the browser, I don't see an error raised. I tried in node as well. No error is raised by the value of undefined is unaffected.
5

This is because undefined is not a reserved word in JavaScript, even though it has a special meaning. So it can be assigned a value and the whole statement is valid. Whereas true and null are reserved words and can't be assigned values.

For reference: JavaScript Reserved Words

Comments

0

I quote directly from the MD Js docs.

While it is possible to use it as an identifier (variable name) in any scope other than the global scope (because undefined is not a reserved word), doing so is a very bad idea that will make your code difficult to maintain and debug.

//DON'T DO THIS

// logs "foo string"
(function() { var undefined = 'foo'; console.log(undefined, typeof undefined); })();

// logs "foo string"
(function(undefined) { console.log(undefined, typeof undefined); })('foo');

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/undefined

Comments

-1

You can not declare a variable with the name of reserved words(keywords) in any language either it's scripting or programming.

if you want to use this you can use _ underscore at the very first of the varibale name.

like: _true , _null or _anythingwhatyouwant

2 Comments

Better to use decent naming conventions instead of hacking out the reserved keywords which is not bad, but might lead to confusion later.
But undefined is not a reserved word, so this answer is totally unrelated to the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.