简体   繁体   中英

Why does assigning Infinity in JavaScript not throw an error, but not work?

Why doesn't infinity give an error when attempted to be reassigned, but is not assignable?

var x = 3;

x = 1; // good, normal
true = 3; // error, normal

Infinity = 4; // no error

console.log(Infinity); // Infinity

As you can read here :

Infinity is a property of the global object, or in other words, a variable in global scope .

Since it's a global property you can access it and assign any value. However assigning a value to a non Writable property doesn't raise any exception, when you are in a not strict mode , and doesn't change the value of the property.

If you use "strict mode", then an exception would be raised.

 "use strict" Infinity = 4;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM