...

/

Untitled Masterpiece

In this lesson, you will learn how variables and scope work in modern JavaScript.

Variables

A variable is a named container with a unique name for storing data values. The statement below declares a variable with the name “car”:

let car;

console.log(car); // undefined

In JavaScript, variables are initialized with the value of undefined when they are created. You can assign a value to a variable using the assignment operator (=) when you declare it:

let car = 'Volvo';

console.log(car); // Volvo

Always initialize your variables before you try using them or you will get an error:

console.log(car); // ReferenceError: car is not defined

let car = 'Toyota';

ECMAScript 2015 (or ES6) introduced two new ways of declaring variables, let and const. These new keywords were introduced to fix the bugs caused by the confusing function scoping of var.

Scopes

By scope, we mean the visibility ...