JS Variables
In this page:
var variableName = value;
let variableName = value;
const variableName = value;
var, let, and const at a Glance
var is the original way to declare a variable, function-scoped and somewhat loose in its rules, while let and const, introduced later, are block-scoped and offer more predictable behavior for modern code.
Example: var, let, and const at a Glance
// Declare the variable `oldStyle`, set to "function-scoped"
// Declare the variable `oldStyle`, set to "function-scoped"
var oldStyle = "function-scoped";
// Declare the variable `modern`, set to "block-scoped"
// Declare the variable `modern`, set to "block-scoped"
let modern = "block-scoped";
// Declare the constant `fixed`, set to "block-scoped, cannot reassign"
// Declare the constant `fixed`, set to "block-scoped, cannot reassign"
const fixed = "block-scoped, cannot reassign";
// Print `oldStyle, modern, fixed` to the console
// Print `oldStyle, modern, fixed` to the console
console.log(oldStyle, modern, fixed);
Declaration and Initialization
Declaration is the act of creating a variable's name, while initialization is giving it a first value, and JavaScript lets you do both together or declare a variable first and assign it a value later.
Example: Declaration and Initialization
let city; // declaration
city = "Paris"; // initialization
console.log(city);
Naming Rules
Variable names must start with a letter, underscore, or dollar sign, can contain numbers after the first character, cannot use reserved keywords, and are case sensitive.
Example: Naming Rules
// Declare the variable `_underscoreStart`, set to `1`
// Declare the variable `_underscoreStart`, set to `1`
let _underscoreStart = 1;
let $dollarStart = 2;
// Declare the variable `tutorialCount`, set to `3`
// Declare the variable `tutorialCount`, set to `3`
let tutorialCount = 3;
// Print `_underscoreStart, $dollarStart, tutorialCount` to the console
// Print `_underscoreStart, $dollarStart, tutorialCount` to the console
console.log(_underscoreStart, $dollarStart, tutorialCount);
Hoisting
Hoisting is JavaScript's behavior of processing variable declarations before running the code line by line, but var and let/const behave very differently, var is hoisted and initialized as undefined, while let and const are hoisted but remain inaccessible until their declaration line runs.
Example: Hoisting
console.log(hoistedVar); // undefined, not an error
var hoistedVar = 5;
Choosing Between var, let, and const
In modern JavaScript, const is the default choice for any value that won't be reassigned, let is used when a value genuinely needs to change over time, and var is generally avoided in new code due to its looser, less predictable scoping rules.
Example: Choosing Between var, let, and const
const pi = 3.14159; // never reassigned
let counter = 0; // will change
counter = counter + 1;
console.log(pi, counter);
- Using var out of habit in new code, when let and const offer safer, more predictable scoping in modern JavaScript.
- Trying to use a variable before it has been declared, which behaves differently depending on whether var, let, or const was used.
- Choosing vague variable names like data or temp, which make code much harder to understand months later.
- A variable is a named container for a value, created with var, let, or const.
- Declaration creates the variable, and initialization gives it its first value, they can happen together or separately.
- Variable names must follow specific rules and, due to hoisting, var and let/const behave differently before their declaration line.
var, let, and const are all supported in every modern browser, though let and const require a reasonably modern engine, standard since 2015 and universal today.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: