Which Characters Are Allowed in JavaScript Variable Names?

In JavaScript, a variable name:

  • Can begin with a letter, underscore (_) or dollar sign ($);
  • Can only contain letters, numbers, underscores, or dollar signs;
  • Cannot be the same as a JavaScript reserved word;
  • Cannot contain spaces;
  • Is case-sensitive.

For example, the following are all valid variable names:

const foo = 'bar';
const $foo = 'bar';
const _foo = 'bar';
const _123foo = 'bar';
const foo_bar123$ = 'bar';
const fooBar = 'bar';

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.