Skip to content

Instantly share code, notes, and snippets.

@tejpratap46
Created July 21, 2016 12:36
Show Gist options
  • Save tejpratap46/ed2efe54a2b19e247f5f4ca85fa97b3c to your computer and use it in GitHub Desktop.
Save tejpratap46/ed2efe54a2b19e247f5f4ca85fa97b3c to your computer and use it in GitHub Desktop.
Check if string is valid as a variable. It is useful for selection username etc.
function isVarName(str) {
'use strict';
if (typeof str !== 'string') {
return false;
}
try {
new Function('var ' + str)();
} catch (e) {
return false;
}
return true;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment