TetSharing
JavaScript tutorial · 19 chapters
  1. Get started
  2. let, const and var
  3. Data types
  4. Operators and equality
  5. Strings
  6. Template literals
  7. if, else and switch
  8. Loops
  9. Functions
  10. Arrow functions
  11. Arrays
  12. map, filter, reduce
  13. Objects
  14. Destructuring and spread
  15. Classes
  16. Errors and try...catch
  17. Promises
  18. async and await
  19. JSON

Final JavaScript quiz
Cheat sheet: javascript array methods

Chapter 2 of 19 · By Ethan Cole · Updated November 21, 2026

JavaScript Variables: let, const and var Explained

A variable is a named slot for a value. Modern JavaScript has two keywords for creating one: const for names that will not be reassigned, and let for names that will. A third, var, is older and best avoided in new code.

const by default, let when needed

const site = "TetSharing"; binds the name site permanently in its scope. Trying to assign to it again throws a TypeError, which the example catches so you can see the error name. let visits = 1; can be reassigned as often as you like: visits = visits + 1 or the shorthand visits += 10.

A good habit is to start with const and switch to let only when you really reassign. Readers then know that a const name means the same thing everywhere it appears.

const is not "frozen"

const protects the binding, not the value. A const array can still have items pushed into it, and a const object can have properties changed, because you are modifying the same object rather than pointing the name somewhere else. To prevent changes to the object itself, use Object.freeze().

JavaScript · editable

Expected output

TetSharing 12
["JS", "SQL"]
TypeError
block scoped
undefined

Block scope

let and const are block scoped: a name declared inside { ... }, including the body of an if or a loop, exists only inside those braces. After the block, inner is unknown, so typeof inner reports "undefined". (typeof is one of the few operations that does not throw for an undeclared name.) Using a let name before its declaration line throws a ReferenceError; the region before the declaration is called the temporal dead zone.

Why not var?

var is function-scoped instead of block-scoped, and its declarations are hoisted to the top of the function with the value undefined. A var inside a loop leaks out of it, which causes subtle bugs. You will still see var in older tutorials and code bases, so recognize it, but write let and const.

Names may contain letters, digits, _ and $, cannot start with a digit, and are case-sensitive. The convention is camelCase: maxSpeed, userName.

Common mistakes

  • Reassigning a const, which throws TypeError: Assignment to constant variable.
  • Declaring the same let name twice in one scope, which is a SyntaxError.
  • Assigning without any keyword. In sloppy mode this silently creates a global variable; in strict mode it throws.

Summary

  • Use const by default and let for values you reassign.
  • const stops reassignment, not changes to an object's contents.
  • let and const are block scoped; var is function scoped and legacy.

Quiz: let, const and var

1. What happens when you run const a = 1; a = 2;?

Assigning to a const binding throws a TypeError at runtime.

2. Can you push to an array declared with const?

const protects the binding, not the contents of the object.

3. Where is a let variable declared inside an if block visible?

let and const are block scoped.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.