JavaScript : Rules for Valid Variable Names

Hemil Patel
2 min readApr 30, 2019

Valid Variable Names

3 Simple Rules

When you start learning JavaScript, the first thing you need to learn is how to define a variable. Prior to that you need to know what is a valid variable name. Not following the protocols of a valid variable name results in a syntactical error. So, what defines a valid variable name?

There are many different ways you can define a variable. I’ve simplified three rules to begin with:

  • Variable names cannot be a keyword. For example, word ‘let’ is considered as a keyword in JavaScript. Hence, you cannot define a variable called ‘let’. The following code snippet results in an error.
let let = ‘do not use let as keyword’ // invalid variable name
  • Variable names cannot start with a number. For example, you may consider to define a variable name as ‘a1’ instead of ‘1a’.
let 1a = 1; // invalid
let a1 = 1; // valid
  • Variable names cannot contain special characters. The only exception is a $ (dollar symbol) and _ (underscore).
let $ = 1; // valid
let $_ = 1; // valid
let __ = 1; // valid
let % = 1; // invalid
let # = 1; // invalid

Other Rules

--

--

Hemil Patel
Hemil Patel

Written by Hemil Patel

I am a front-end programmer and I love JavaScript!

No responses yet