According to this page https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Reserved_Words
You can’t use these reserved words as variables, but can you use them this way apperantly:
var test = {
break : 'whatever',
case : 'whatever',
if : 'whatever',
in : 'whatever'
//etc
};
I have a fiddle here with all the values http://jsfiddle.net/9RQ9j/4/
And I get no errors either defining them or calling for instance like this
console.log(test.if)
So to me it seems possible, but is it in conflict with the javascript spec? Then it seems at least like all the browsers I have tested in (which are most) do violate the spec and allow you to use those words.
(UPDATE: seems to fail in IE 8 + possibly other IE’s)
And is it even good practice?
Last question: Is this ok?
var test = {
'break' : 'whatever',
'case' : 'whatever',
'if' : 'whatever',
'in' : 'whatever'
};
According to the ES5 Spec, object properties are IdentifierNames, not Identifiers, which means that they do not exclude reserved words.
Is it good practice? Probably not in general, but if it makes your code clearer, then yes.
And yes, referencing properties with a quoted string is equivalent to referencing them with the same unquoted symbol — but the set of identifiers that you can use quoted is obviously larger than those you can use unquoted.