简体   繁体   中英

Is it possible to force the boolean true to equal false in javascript?

Is there a way to make true = false . When I try that, it obviously returns an invalid left-hand assignment. This is something that I have been curious about for a while, and I would appreciate an answer. I have tried a lot of things, but I haven't figured out how to do it. Here's some things that I've tried so far:

true = false;
var true = false;
let true = false;
const true = false;
Boolean(true) = false;
true = !true;
var true = !true;
let true = !true;
const true = !true;
Boolean(true) = !true;

Because true and false are reserved keywords, there's no way to assign to them. Even if you did window.true = false , true would still be true.

This isn't necessarily the case with all primitives. in es4 you were allowed to reassign undefined to be whatever you want it to be. (see this related discussion - it talks about weather or not to bother safe-guarding against it, but it you could find it informative)

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