Variables

This YouTube video was created by Steve Griffith.

One of most basic component of any programming language is a variable. A variable is container for a value and given an identifier. In most cases the value of the variable can change, while the identifier will stay the same.

Declaring a Variable

This Scrimba screencast was created by Dylan C. Israel.

In JavaScript, a variable must be declared before it can be used. A variable is declared by using a declaring statement followed by the variable identifier.

Depending on the declaring statement used the declared variable can or must be assigned a value. The equal sign ( = ) is the variable assignment operator. There are 3 declaring statements in JavaScript, var, let, and const.

var vs let vs const

In 1995, when JavaScript was first created, it had only one declaring statement var. For two decades it was the de facto way of declaring variables. Then in July 2015, the version of JavaScript known as ES2015 or ES6 was released and with it came two new declaring statement, let and const.

Each declaring statement will cause the variable that declared with it to act a little different. Below is a brief explain of those differences.

var

The var statement is used to declare a variable that has function scope or is accessible anywhere within a function. If declared outside of a function it will be given global scope and be added to the global object. Variables declared with var do not need to be assigned a value, can be re-assigned a value, and can be redeclared without an error.

let

The let statement is used to declare a variable that has block scope or is accessible anywhere within a block or a set of curly braces. If declared outside of a function or block it will be given global scope but is NOT added to the global object. Variables declared with let do not need to be assigned a value, can be re-assigned a value, but CANNOT be redeclared and will result in an error.

const

The const statement is used to declare a variable that has block scope or is accessible anywhere within a block or a set of curly braces. If declared outside of a function or block it will be given global scope but is NOT added to the global object. Variables declared with const MUST to be assigned a value, CANNOT be re-assigned a value, and CANNOT be redeclared. If any of these situation occur it will result in an error.

Data Types

Variables can hold different values and these values can be broken down into different categories known as Data Types. In JavaScript, data types are divided into two categories Primitive Data Types and Objects.

Primitive Data Types

The primitive data types are as follows:

Primitive data types define immutable values or value that are cannot be changed. This means the specific value cannot change, not the variable holding that value. For example, value of the Number 2 is always 2, you cannot change that.

Objects

In JavaScript, an object consider to be a collection of properties. These properties are stored in a key value pairs. The key is used to identified properties, and values can be a value of any type.

Any data structure that is not a primitive data type is an object including:

To learn more review the documentation on the Mozilla Developer Network on JavaScript Data Types and Data Structures.