Search⌘ K
AI Features

JavaScript Variables

Explore JavaScript variables by understanding how var let and const differ in declaration, updating, and redeclaration. Learn the scope rules for each and how hoisting affects variable accessibility. This lesson helps you write modern, predictable JavaScript using ES6 standards.

By the end of this lesson, we’ll be able to understand the differences between var, let, and const and how to use them correctly.

In JavaScript, variables can be declared with var, let, or const.

Note: The let and const keywords were introduced in ES6 and are the preferred variable declaration methods for this course.

Variable update

The var variables can be updated and redeclared within its scope.

Let’s take a look at the code below. What will be logged to the console? Why?

Javascript (babel-node)
var color = "red";
var color = "yellow";
console.log(color);

The value yellow is logged because var ...