Variables

Learn about the properties of a variable and how to declare it in JavaScript.

Role of a variable

A computer program stores data using variables. A variable is an information storage area. We can imagine it as a box in which you can put and store things!

Variable properties

A variable has three main properties:

  • Its name, which identifies it. A variable name may contain upper and lower case letters, numbers (not in the first position), and characters like the dollar sign ($) or underscore (_).
  • Its value, which is the data stored in the variable.
  • Its type, which determines the role and actions available to the variable.

You don’t have to define a variable type explicitly in JavaScript. Its type is deduced from the value stored in the variable and may change while the program runs. That’s why we say that JavaScript is a dynamically typed language. Other languages, like C or Java, require variable types to always be defined. This is called static typing.

Declaring a variable

Before you can store information in a variable, you have to create it! This is called declaring a variable. Declaring a variable means the computer reserves memory in which to store the variable. The program can then read or write data in this memory area by manipulating the variable.

Here’s a code example that declares a variable and shows its contents:

Get hands-on with 1200+ tech skills courses.