Sari la conținutul principal
Frontend este un bonus gratuit pe CodeMentor AI — produsul principal este Python. Înregistrează-te pentru a-ți salva progresul și a debloca toate cele 299 de lecții Frontend + 1,100+ lecții Python.Înregistrează-te — gratuit
← ⚡ JavaScript & the browser·Modul C1 · Lecția 2
SarcinăDeclare three names: const APP_NAME set to 'CodeMentor', let counter set to 0, and inside a separate { } block use let inside set to 'private'. After the block, log APP_NAME, counter, and a literal string 'done' (do not log 'inside' — that should not be in scope).

let și const: nu folosiți niciodată var

75 XP6 minFREE
Teorie

Two ways to name a value in 2026

const PI = 3.14;       // I'm never reassigning this
let counter = 0;       // I will reassign this
counter = counter + 1; // OK

Rules of thumb that hold up in production:

  1. Reach for `const` first. If you're not planning to reassign, use const. It's the safer default.
  2. Use `let` when the value genuinely changes — a loop counter, a "current" pointer, a value built up step by step.
  3. Never use `var`. It exists for backward compatibility. var ignores block scope, leaks into surrounding functions, and is hoisted in ways that hide bugs. If you see it in code from before 2017, that's why.

const doesn't mean immutable

A common surprise:

const user = { name: "Anna" };
user.name = "Boris";   // OK, the OBJECT is mutated
user = {};             // ERROR, the BINDING can't be reassigned

const locks the binding — what variable name points to. It doesn't freeze the value inside. Object.freeze() is the next-level lock if you need that.

Block scope, not function scope

if (true) {
  let inner = 10;
}
console.log(inner);   // ReferenceError: inner is not defined

The {} braces are the boundary. var would have leaked inner to the surrounding function. let doesn't.

2 file
Previzualizare live
console output appears here…
← AnterioarăLecția următoare →

Primește un sfat Python sau web pe zi — prin email

Scurt, scris de mână, fără spam. Dezabonare cu un click.