🌱BeginnerJavaScript Fundamentals

Variables and Constants

Declaring variables with var, let, and const and knowing which to use.

Overview

A variable is a named container for storing a value. In JavaScript there are three keywords for declaring variables: var, let, and const. While all three exist in the language, modern JavaScript strongly favors let and const, and you should treat var as legacy code. const (constant) is for values that will not be reassigned. Once you assign a value to a const, trying to reassign it throws a TypeError. Use const by default it makes your code easier to reason about because readers know the variable won't change. let is for values that will change over the lifetime of your program a counter, a running total, a flag that flips between true and false. Use let when const would be too restrictive. var is function-scoped (not block-scoped like let/const), gets hoisted to the top of its function, and allows redeclaration in the same scope all of which cause subtle bugs. Avoid it in new code.

Key Points

  • •Use const by default switch to let only when you know the value must change
  • •const prevents reassignment, not mutation: a const array can still have items pushed to it
  • •let and const are block-scoped: they only exist inside the { } block where they are declared
  • •var is function-scoped and hoisted it causes confusion and bugs, avoid in new code
  • •Variables declared without any keyword become implicit globals never do this
  • •Naming convention: camelCase for variables (myCount), SCREAMING_SNAKE_CASE for true constants (MAX_RETRIES)

Examples

const vs let when to use each

const PI = 3.14159;         // never changes
const name = "Alice";       // never reassigned

let count = 0;              // will increment
count = count + 1;
console.log(count);         // 1

// PI = 3; would throw TypeError: Assignment to constant variable

Block scoping with let and const

let x = 10;

if (true) {
  let x = 20;        // different variable, scoped to this block
  console.log(x);    // 20
}

console.log(x);      // 10 outer x is unchanged

With var, both console.log calls would print 20 because var ignores block scope.

const with objects and arrays (mutation is still allowed)

const numbers = [1, 2, 3];
numbers.push(4);              // OK mutating the array
console.log(numbers);         // [1, 2, 3, 4]

// numbers = [5, 6, 7];      // ERROR reassigning the variable

Common Mistakes

  • Using var in new code use let/const instead
  • Thinking const makes an object or array immutable it only prevents reassignment of the variable itself
  • Declaring variables with let inside loops when const would work in the loop body (const is fine in for...of, for...in)

Note

A good rule of thumb: always start with const. If your code throws an error because you need to reassign the variable, change it to let. You'll find most variables end up as const.

Exercises

Write JavaScript code to solve each exercise. Run it and check your output.

Loading editor…

Completed: 0 / 5 exercises

Knowledge Check

Question 1 of 4

Which keyword should you use by default when declaring a variable in modern JavaScript?