is-var-name
Check if a string can be used as a JavaScript variable name
; //=> true; //=> false
Installation
npm install is-var-name
API
;
isVarName(name)
name: string
Return: boolean
It returns true
if the string can be used as a valid JavaScript identifier name. If not, or the argument is not a string, it returns false
.
; //=> true; //=> true ; //=> false; //=> false; //=> false; //=> false ; //=> false; //=> false
Another solution
Instead of this module, you can use the regular expression that matches valid variable names.
Regular expression pros
is-var-name uses Function
constructor but regular expression doesn't.
According to the ESLint documentation, new Function()
is:
considered by many to be a bad practice due to the difficult in debugging and reading these types of functions.
Regular expression cons
Since the regular expression is too long (about 16,000 characters), it increases the file size of your library or application.
License
ISC License © 2018 Shinnosuke Watanabe