A variable is a way to store information so that you can later use and manipulate it. For example, imagine a JavaScript-based pinball game where the goal is to get the highest score. When the player first starts the game, their score will be zero, but as they hit targets with the pinball, the score will get bigger. In this case, the score is a variable since it starts at 0 but increases as the game goes on.

Think of a variable as a basket: you can put an item into a basket, look inside the basket, dump out the contents of the basket, or even replace what’s in the basket with something else. However, even though you might change what’s inside the basket, it still remains a basket.

Creating a Variable

Creating a variable is a two-step process that involves declaring the variable and naming it. In JavaScript to create a variable named “score”, you would type:

var score;

The first part, “var”, is a JavaScript keyword that creates, or, in programming-speak, declares the variable. The second part of the statement, “score”, is the variable’s name.

What you name your variables is up to you, but there are a few rules you must follow when naming variables:

  • Variable names must begin with a letter, $, or _.
    In other words, you can’t begin a variable name with a number or punctuation: so “1thing” and “&thing” won’t work, but “score”, “$score”, and “_score” are fine.
  • Variable names can only contain letters, numbers, $, and _.
    You can’t use spaces or any other special characters anywhere in the variable name: “fish&chips” and “fish and chips” aren’t legal, but “fish_n_chips” is.
  • Variable names are case-sensitive.
    The JavaScript interpreter see uppercase and lowercase letters as distinct, so a variable name “SCORE” is different from a variable named “score”, which is also different from “sCoRE” and “Score”.
  • Avoid keywords.
    Some words in JavaScript are specific to the language itself. “var” for example is used to create a variable, so you can’t name a variable “var”. In addition, some words, like “alert”, “document”, and “window” are considered special properties of the Web browser. You’ll end up with a JavaScript error if you try to use those words as variable names. You can find a list of some reserved words in the table below. Not all of these reserved words will cause problems in all browsers, but it’s best to stay clear of them when naming variables.
JavaScript keywords
Reserved for future use Reserved for browser
break
case
catch
continue
default
delete
do
else
finally
for
function
if
in
instanceof
new
return
switchthis
throwtry
typeofvar
voidwhile
with
abstract
boolean
byte
char
class
const
debugger
double
enumexport
extends
final
float
goto
implements
import
int
interface
long
native
package
private
protected
public
short
staticsuper
synchronized
throws
transient
volatile
alert
blur
closed
document
focus
frames
history
innerHeight
innerWidth
length
location
navigator
open
outerHeight
outerWidth
parent
screen
screenX
screenY
statusbar
window