Variables and Data Types

let, const, and common JavaScript types

Back to Exercises

Declarations

let (mutable)

const (immutable reference)

// let (can be reassigned) let count = 1; count = 2; // OK // const (cannot be reassigned) const apiBase = 'https://api.example.com'; // apiBase = 'x'; // Error

Primitive Types

const text = 'Hello'; // string const age = 21; // number const pi = 3.14; // number const isAdmin = false; // boolean const nothing = null; // null let notDefined; // undefined const big = 9007199254740991n; // bigint const sym = Symbol('id'); // symbol

Reference Types

const user = { name: 'Ana', email: 'ana@example.com' }; // object const colors = ['red', 'green', 'blue']; // array // Mutating contents of const object is allowed user.name = 'Anne'; colors.push('yellow');

Full Code Used in This Demo

HTML

<button onclick="demoLet()">Run let Example</button>
<div id="out-let"></div>
<button onclick="demoPrimitives()">Show Primitives</button>
<div id="out-primitives"></div>
<button onclick="demoReferences()">Show References</button>
<div id="out-references"></div>

CSS

.btn { background: #3b82f6; color: #fff; border-radius: 8px; }

JavaScript

// let (can be reassigned)
function demoLet() {
  let count = 1;
  count = 2;
  document.getElementById('out-let').textContent = `let count starts 1 → reassigned to ${count}`;
}

// const (cannot be reassigned)
function demoConst() {
  const base = 'https://api.example.com';
  document.getElementById('out-const').textContent = `const base = ${base} (cannot be reassigned)`;
}