Is it possible in Javascript to determine if a certain string is a reserved language keyword such as switch
, if
, function
, etc.?
What I would like to do is escaping reserved identifiers in dynamically generated code in a way that doesn't break on browser-specific extensions.
The only thought coming to my mind is using eval
in a try-catch block and check for a syntax error. Not sure how to do that though. Any ideas?
Asked
Active
Viewed 2,577 times
1

GOTO 0
- 42,323
- 22
- 125
- 158
-
5I'd just go [here](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Reserved_Words), grab all the reserved words put them in an array and check the string. – elclanrs Apr 22 '13 at 22:12
-
What's the context of this question? Are you dynamically creating variable names, or just want to make sure when coding that you don't accidentally use a reserved word (in which case, using an IDE or something like Notepad++ will do). Or use jsLint to check for this stff. – David Gilbertson Apr 23 '13 at 01:02
3 Answers
6
One option would be to do:
var reservedWord = false;
try {
eval('var ' + wordToCheck + ' = 1');
} catch {
reservedWord = true;
}
The only issue will be that this will give false positive for words that are invalid variable names but not reserved words.
As pointed out in the comments, this could be a security risk.

Jonah
- 15,806
- 22
- 87
- 161
-
4
-
1That's a good point, but depending on context it might not be an issue. I think the solution of just getting the full list is best, but OP asked how you'd do it this way so I figued I'd answer. – Jonah Apr 22 '13 at 22:17
-
you could get false negatives as well, as some browsers let you use some keywords as variable names. – Reason Apr 22 '13 at 22:19
-
1@Reason, which browsers and which words, out of curiosity? I thought the ECMA script specification disallowed that. – Jonah Apr 22 '13 at 22:21
-
This is quite close to what I was thinking about, although I wouldn't mind doing the check in a different way if that's better for a reason. – GOTO 0 Apr 22 '13 at 22:26
-
@Jonah Don't remember exactly, but it was on of those keywords that are not really used in JS, but are reserved in Java. I think it worked in most browsers except Chrome for Android, where my code stopped working. – Reason Apr 22 '13 at 22:29
-
4How about `eval('(function(' + wordToCheck + '){})')`. This will not pollute the current scope (imagine `wordToCheck=='reservedWord'`...). Also, to avoid security issues, just add a test like `/^[a-z]+$/i.test(wordToCheck)` – user123444555621 Apr 23 '13 at 05:22
1
I guess you could solve it using eval, but that seems like sort of a hack. I would go for just checking against all reserved words. Something like this:
var reservedWords = [
'break',
'case',
...
];
function isReservedWord(str) {
return !!~reservedWords.indexOf(str);
}
Here is a list of all reserved words: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Reserved_Words
Also, a problem with the eval-approach is that some browsers sometimes allows you to use some reserved words as identifiers.

Reason
- 1,410
- 12
- 33
-
1No need for that object. An array should do, if the value exists in the array then it's a reserved word. `return !!~reservedWords.indexOf(str);` – elclanrs Apr 22 '13 at 22:18
-
1
1
FYI, Some words throws error only in strict
mode
ex: package
eval('var package = 1') // No error
'use strict'
eval('var package = 1') // throws error

Naren
- 4,152
- 3
- 17
- 28