Home / Lessons / Variables

Variables

JavaScript-logo

Variables are named values and can store any type of JavaScript value.

Here’s how to declare a variable:

EXAMPLE

var x = 100;

And here’s what’s happening in the example above:

Using variables

After you declare a variable, you can reference it by name elsewhere in your code.

EXAMPLE

var x = 100;
x + 102;

OUTPUT

202

You can even use a variable when declaring other variables.

EXAMPLE

var x = 100;
var y = x + 102;
y;

OUTPUT

202

Reassigning variables

You can give an existing variable a new value at any point after it’s declared.

EXAMPLE

var weather = "rainy";
weather = "sunny";
weather;

OUTPUT

"sunny"

Naming variables

Variable names are pretty flexible as long as you follow a few rules:

With that in mind, here are valid variable names:

EXAMPLE

var camelCase = "lowercase word, then uppercase";
var dinner2Go = "pizza";
var I_AM_HUNGRY = true;
var _Hello_ = "what a nice greeting"
var $_$ = "money eyes";

And here are some invalid variable names — try to spot what’s wrong with each of them:

EXAMPLE

var total% = 78;
var 2fast2catch = "bold claim";
var function = false;
var class = "easy";

Variable names are case-sensitive, so myVar, MyVar, and myvar are all different variables. But generally, it’s a good practice to avoid naming variables so similarly.

Reference: https://www.javascript.com/learn/variables


Previous: Lessons
Next: Functions