is-identifier
The `is-identifier` package provides a highly focused utility to determine if a given string constitutes a valid JavaScript identifier according to ECMAScript specifications. Currently at stable version 1.0.1, this package follows Sindre Sorhus's typical release cadence of infrequent, targeted updates, primarily for bug fixes or minor spec clarifications, ensuring long-term stability. Its key differentiators include precise adherence to the JavaScript identifier rules, including reserved keywords (like `await`) and special global properties (like `globalThis`, `Infinity`, `NaN`, `undefined`) which it correctly treats as invalid identifiers for practical use, despite some not being strictly "keywords". It is a small, dependency-free module optimized for performant identifier validation, contrasting with broader AST parsing libraries.
Common errors
-
TypeError: (0 , is_identifier__WEBPACK_IMPORTED_MODULE_0__.default) is not a function
cause Incorrectly attempting to destructure the default import in some bundler configurations, or using a named import when the package only provides a default export.fixEnsure you are using `import isIdentifier from 'is-identifier';` (ESM) or `const isIdentifier = require('is-identifier');` (CommonJS) as it is a default export. -
ReferenceError: isIdentifier is not defined
cause Attempting to use `isIdentifier` without properly importing or requiring it, or using an incorrect import/require syntax.fixVerify your import statement: `import isIdentifier from 'is-identifier';` for ESM, or `const isIdentifier = require('is-identifier');` for CommonJS environments.
Warnings
- gotcha TypeScript users upgrading from v1.0.0 might observe changes in type inference or validation. Version 1.0.1 removed an incorrect TypeScript type guard, which corrects the library's type behavior to align precisely with its runtime logic and ECMAScript identifier rules.
- gotcha The library intentionally treats certain global properties (e.g., `globalThis`, `Infinity`, `NaN`, `undefined`) as invalid identifiers. While not strictly reserved keywords, using them as identifiers is generally ill-advised and this package enforces that practical restriction.
- gotcha The `await` keyword is correctly recognized and treated as a reserved identifier, causing `isIdentifier('await')` to return `false`. This holds true even in contexts where `await` might be valid within an `async` function, as the check is for general identifier validity.
Install
-
npm install is-identifier -
yarn add is-identifier -
pnpm add is-identifier
Imports
- isIdentifier
import { isIdentifier } from 'is-identifier';import isIdentifier from 'is-identifier';
- isIdentifier (CommonJS)
const { isIdentifier } = require('is-identifier');const isIdentifier = require('is-identifier');
Quickstart
import isIdentifier from 'is-identifier';
// Basic valid identifiers
console.log(`'foo' is identifier: ${isIdentifier('foo')}`);
console.log(`'$bar' is identifier: ${isIdentifier('$bar')}`);
console.log(`'_baz' is identifier: ${isIdentifier('_baz')}`);
console.log(`'myVariable123' is identifier: ${isIdentifier('myVariable123')}`);
// Invalid identifiers
console.log(`'1kg' is identifier: ${isIdentifier('1kg')}`);
console.log(`'await' is identifier: ${isIdentifier('await')}`); // Reserved keyword
console.log(`'my-variable' is identifier: ${isIdentifier('my-variable')}`);
console.log(`'function' is identifier: ${isIdentifier('function')}`); // Reserved keyword
// Special global properties treated as non-identifiers
console.log(`'globalThis' is identifier: ${isIdentifier('globalThis')}`);
console.log(`'Infinity' is identifier: ${isIdentifier('Infinity')}`);
console.log(`'NaN' is identifier: ${isIdentifier('NaN')}`);
console.log(`'undefined' is identifier: ${isIdentifier('undefined')}`);