Which characters can be used for naming a JavaScript variable?
I want to create a small \"extension library\" for my non-JavaScript users here at work (who all seem
in case regular expressions is not a must, wouldn't it be better to just ask the browser to decide using eval
?
function isValidVarName( name ) {
try {
// Update, previoulsy it was
// eval('(function() { var ' + name + '; })()');
Function('var ' + name);
} catch( e ) {
return false;
}
return true;
}
isValidVarName('my_var'); // true
isValidVarName('1'); // false