1

Is it possible to override the reserved words? I would never do this, but it has caught my attention and made me curious.

Can I make false = true in Javascript? I've seen some pranks on various sites, where people override the default reserved words with the opposite (not Javascript).

Is this possible in Javascript though?

Lemvig Kiggeren
  • 317
  • 1
  • 3
  • 13
  • where did you see this `false=true` atleast for me it gives an error. – Mritunjay Oct 22 '15 at 08:05
  • @Mritunjay The user means actually making `false` the keyword as the value `true`. – Spencer Wieczorek Oct 22 '15 at 08:05
  • 1
    false is value, and can't be converted to true, this is similar if you ask can you set 5 to 2 ( 5 = 2) – Grissom Oct 22 '15 at 08:06
  • What I mean is protected words. Not variables. My bad. I want to know if it's possible to set `false = true`, because I have seen something similar as a prank in another language. – Lemvig Kiggeren Oct 22 '15 at 08:07
  • 1
    It _used_ to be possible to set `undefined` to some other value, but fortunately that's no longer the case. – Alnitak Oct 22 '15 at 08:10
  • @Alnitak `undefined` is not a reserved word – zerkms Oct 22 '15 at 08:11
  • @Grissom on Chrome at least the parser complains if you do `var false = true` because it detects the _keyword_ where a variable identifier would be expected. Only if you just do `false = true` will it complain about finding an rvalue on the LHS. – Alnitak Oct 22 '15 at 08:11
  • @zerkms I never said it was, but it's the closest scenario to what the OP described that I'm aware of. – Alnitak Oct 22 '15 at 08:13
  • @Alnitak "A JavaScript Boolean represents one of two values: true or false." http://www.w3schools.com/js/js_booleans.asp – Grissom Oct 22 '15 at 08:14
  • 1
    If you're gonna keep quoting w3fools at me I'm leaving... – Alnitak Oct 22 '15 at 08:15
  • Wait, you want to say that false is not value? – Grissom Oct 22 '15 at 08:15
  • 1
    @Grissom those are "boolean literals", if you want to give it some precise name as per the standard. – zerkms Oct 22 '15 at 08:16
  • I found another oddity with `undefined` - if you do `a = (undefined = 5)` then the result of the RHS assignment operator in the parentheses is still 5 and that's what's assigned to `a`, even though `undefined` is not modified. – Alnitak Oct 22 '15 at 08:19
  • @Alnitak that's because the assignment operator returns the `rval` and in non-strict mode the operation is successful (even though it does not modify the `undefined`) – zerkms Oct 22 '15 at 08:22
  • @Alnitak, you can call it as you want, but you can't still prove that is possible to assign true to false. – Grissom Oct 22 '15 at 08:23
  • @zerkms interesting - I had always assumed that the assignment operator would return the (new) value of the `lval` – Alnitak Oct 22 '15 at 08:23
  • @Alnitak http://www.ecma-international.org/ecma-262/6.0/#sec-assignment-operators-runtime-semantics-evaluation --- see 1h – zerkms Oct 22 '15 at 08:24
  • @Grissom what is your point in this dialogue? – zerkms Oct 22 '15 at 08:24
  • @zerkms yup, just been reading that. I tested it too, by creating an object with a getter and setter on a property and doing `var a = obj.prop = 10` and showing that the getter isn't called (http://jsfiddle.net/alnitak/qbeff8e3/) – Alnitak Oct 22 '15 at 08:31
  • @zerkms, I mean it no makes sense to post examples and links which are not answer on question. – Grissom Oct 22 '15 at 08:36
  • @Grissom these are comments, not answers. Comments can be virtually about anything. Like: it was a nice day here today. – zerkms Oct 22 '15 at 08:36
  • I rest my case, have a nice day! – Grissom Oct 22 '15 at 08:38

2 Answers2

1

No, this is not possible in JS. false and true are literals that will resolve to exactly those values - the same is true for numbers, strings and null.

Moreover, they are reserved words, so you cannot use them as variables names - in contrast to undefined, which you could shadow and (prior to ES5) even overwrite. Also, ES5 refined the workings of array and object literals so that you cannot any more mess with those either.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

Nop, it is not possible to use reserved words as variable in JS : http://www.w3schools.com/js/js_reserved.asp

Finrod
  • 2,425
  • 4
  • 28
  • 38