Console Learner — An interactive JavaScript course.


2.1 Variables & Assignment

>>>
var a, b

  
Variable names are defined using the var keyword. Several variables can be named in a single statement.
>>>
var a = 2

  
Optionally, an initial value can be provided. It is not an error to reuse an existing variable name. It will create a new variable that shadows (hides) the previous one.
>>>
b
undefined
If no value was specified when declaring the variable, undefined is used as the default.
>>>
b = 3
3
The = assignment operator is used to change the value of a variable.
>>>
c = 5
5
Beware! If a variable name hadn't been previously defined with var, a new global variable is created by the assignment. It is a common cause for bugs.
>>>
c += 1
6
The +=, -=, *=, /= and %= modifying assignment operators allows for easy updates. A few others also exist, which we'll see later.
>>>
++c
7
The ++ and -- operators increment or decrement a numeric variable and return its value. If placed after the variable, the previous value is returned (instead of the updated one).
>>>

The console allows you to interact with the course material and examples. Use the following keys:

A special logging function is also available:


Need more magic? Check out the other developer tools on this site.