Select Page

Javascript uses variables as containers for data values. The modern Javascript standard handles variables differently than it did in the past; variables now have more precise scopes than they once did thanks to changes in Javascript ES6.

Whether you create variables using older Javascript conventions (which are still valid) or exclusively use ES6 strategies, the syntax for creating and using variables is still largely the same. Javascript variables also have some quirkiness that may not be obvious at first glance, so you should account for those quirks in any syntax.

This article describes how to create and define Javascript variables with older and newer programming strategies. It also describes how to handle quirks in variable management.

Creating Javascript Variables

Javascript variables are containers for values defined by Javascript data types. The results of Javascript functions, comparisons using Javascript operators, and groups of values like Javascript arrays can all be assigned to variables.

The names of Javascript variables may contain alphanumeric characters, underscores, and dollar signs. They may not contain any special characters or symbols.

Javascript variable names may not begin with a number, but they may begin with letters, underscores, or dollar signs. Reserved Javascript keywords may never be used as names.

Many popular frameworks, such as JQuery, use dollar signs in their variable names to denote specialized properties and methods for that framework. Since that de facto convention has become more common, most programmers don’t use dollar signs in their own code as often; this avoids confusion when using frameworks.

Underscores give useful information through variable names in two different contexts. Since variable names cannot contain spaces, underscores can separate words in variable names to make code clearer. Many programmers also start variable names with underscores if those variables shouldn’t be directly changed.

var _secondsPerYear = ( 365 * 24 * 60 * 60 ) ; // a constant value

Defining Javascript Variables

Define Javascript variable values using the assignment operator (=). If you create a variable without assigning a value, its default value is undefined. Remember that undefined is not the same data type as null.

Versions of Javascript prior to ES2015 (ES6) declared variables using the var keyword. The let and const keywords first appeared in ES2015.

var myVar1;
let myVar2;
const myVar3 = 1;

The var and let keywords allow you to declare a variable without assigning it to a value because both keywords create variables that a program may update later. In contrast, you must assign a value to any variable that uses the const keyword; this keyword creates a constant value that cannot be changed once the variable has been defined.

Javascript Variable Quirks

Javascript variable names are case sensitive. This means that the names “Udacity” and “UDACITY” would be two different variables in a Javascript program. In addition, typos, such as “UDacity,” would also accidentally create another variable. Make sure that your variables are consistently named.

It is possible to create and define multiple variables at the same time. To do this, separate variables with commas. Variable definitions can span multiple lines as well; the following two variable definition groups are equivalent.

let company = "Udacity", author = "JSRC", blog = "variables";

let company = "Udacity",
author = "JSRC",
blog = "variables";


Creating and defining Javascript variables correctly is more complicated than it may first appear, depending on the version of Javascript you are using and your scoping needs. Since all modern web browsers use ES6 conventions, you can rely on them in variable management, but knowing how to use older strategies helps you decipher and modernize older code.

Enroll in our Intro to Programming Nanodegree Program today to learn more about Javascript variables and other programming concepts.

Start Learning

Jessica Reuter Castrogiovanni
Jessica Reuter Castrogiovanni