← Download MyJS

MyJS block

isValidVarName 1.0.1

Determines if a given string can be used as a valid variable name in JavaScript. There is the optional strict mode that can be used

Download with this
Exports
isValidVarName
Works in
Browser and Node.js
Requires
Hidden Function Constructors 1.x
The code of version 1.0.1
/**
 * Tells whether a string can be used as a variable name in JavaScript.
 *
 * It works by asking the JavaScript engine itself: it tries to create a
 * function with `name` as its only parameter, and the name is valid if that
 * doesn't throw a syntax error. Characters that would make it a different kind
 * of parameter list (whitespace, commas, `=` defaults, comments, `{`/`[`
 * destructuring, `...` rest parameters and `@`) are replaced first, so only a
 * single plain name can pass. Results are cached for each mode.
 *
 * Unicode escapes are allowed, as they are in JavaScript (`"\\u0061"` is the
 * name `a`). Globals like `undefined` and `NaN` aren't reserved words, so they
 * are valid names.
 *
 * Note that `Function` and `AsyncFunction` count as `eval`, so on a page whose
 * Content Security Policy doesn't allow `'unsafe-eval'`, every name is reported
 * as invalid.
 *
 * @function isValidVarName
 * @param {string} name
 *   The name to check.
 * @param {boolean} [opt_strictMode=false]
 *   If true, `name` is checked the way an ES module would check it: strict
 *   mode rules (so `eval`, `arguments`, `let`, `static`, `yield`, etc. aren't
 *   allowed) and `await` is reserved too. If false, `name` is checked the way
 *   an ordinary non-strict script would check it, where all of those are
 *   allowed.
 * @returns {boolean}
 *   True if `name` can be used as a variable name in the chosen mode, false if
 *   it can't.
 *
 * @example
 * isValidVarName('typeOf');        // true
 * isValidVarName('café');          // true
 * isValidVarName('2fast');         // false (starts with a number)
 * isValidVarName('{a}');           // false (destructuring, not a name)
 * isValidVarName('class');         // false (always reserved)
 *
 * @example
 * // Words that are only reserved in strict code or modules
 * isValidVarName('let');           // true
 * isValidVarName('let', true);     // false
 * isValidVarName('await');         // true
 * isValidVarName('await', true);   // false
 */
var isValidVarName = (function(STRICT_VARS, NORMAL_VARS, hasOwnProperty) {
  return function(name, opt_strictMode) {
    let TARGET_VARS = opt_strictMode ? STRICT_VARS : NORMAL_VARS;
    if (hasOwnProperty.call(TARGET_VARS, name)) {
      return TARGET_VARS[name];
    }
    let result;
    try {
      result = !!(opt_strictMode ? AsyncFunction : Function)(
        name.replace(/[{\[.\s\/,=@]/g, ')') || ')',
        opt_strictMode ? '"use strict";' : ''
      );
    } catch (e) {
      result = false;
    }
    return TARGET_VARS[name] = result;
  };
})({}, {}, ({}).hasOwnProperty);

Versions

  1. 1.0.1 Oct 16, 2026

    Now requiring the hidden function constructors instead of declaring a private version of AsyncFunction.

  2. 1.0.0 Oct 16, 2026

    Initial

JSDoc