Assigning undefined value in javascript

I wrote code in javascript. When I accidentally stumbled upon this.

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

true = 'some value';   //gives error

null = 'some value';   //gives error

how is the first statement valid, while the other two are invalid. From what I know as undefined, true and null are values ​​that you can assign to some variable, so all this should be invalid operations.

+4
source share
4 answers

From MDN :

undefinedis a property of a global object; that is, it is a variable on a global scale. The initial value undefinedis a primitive value undefined.

, undefined true null, . , NaN, , , , .


, , undefined, , readonly.

MDN.

(JavaScript 1.8.5/Firefox 4+) undefined , ECMAScript 5 . , .

enter image description here


JavaScript, "use strict" , . -

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

[1] VM1082: 2 Uncaught TypeError: 'undefined' '#'

+5

, undefined JavaScript, . , . true null .

: JavaScript

+3

( ) , , .

, _ varibale.

like: _true, _null _anythingwhatyouwant

0

MD Js.

( ) ( undefined ), , .

//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

0

Source: https://habr.com/ru/post/1680860/


All Articles