Skip to content

Variable Declarations: let, const, var

In ES6, JavaScript introduced let and const as new ways of declaring variables. Here's the TLDR for when to use each.

let: Use let if the variable needs to change:

js
let name = 'John';
name = 'Bob';

const: Use const for constant variable that doesn't need to change

js
const DEFAULT_TEMP = 60;
const person = { name: 'Bob' };

DEFAULT_TEMP = 30; // Uncaught TypeError: Assignment to constant variable.

var: Traditional way of declaring variables.

js
function oldSchool() {
	tradition = false; // hoisted and allowed
	console.log(tradition); // false
}
oldSchool();
var tradition;
console.log(tradition) // false

In practice:

  • Use const by default.
  • Use let if the variable needs to be reassigned.
  • Don't use var.