Please enable JavaScript to use CodeHS

CodeHS Glossary


Declare a Variable JavaScript

Declaring a variable is defining it for the first time.

When declaring a variable in Javascript, you use var in front.


For example, this creates a variable named numApples that has no value yet.

var numApples;

Later, you can initialize it by typing

numApples = 2;

which sets the value of numApples.


This declares and initializes a variable numOranges with a starting value of 4.

var numOranges = 4;

Declaring is different from updating the value of a variable. Here’s an example:

var points = 5;  // declares the points variable and sets its value to 5
points = 10;     // updates the value of the points variable to 10