## glossary

What is Variables?

A variable is a space in memory identified by a name, used to store a value that can be read and — depending on how it was declared — changed while the program runs. It is the most fundamental concept in any programming language.

The most common analogy is the labeled box: you store something inside and use the label to find it later. The label is the name; the content is the value.

Declaring in JavaScript: var, let and const

Three keywords with distinct behaviors
var antigo = 'function scope, avoid in new code';

let contador = 0;      // can be reassigned
contador = 1;          // valid

const TAXA = 0.15;     // cannot be reassigned
TAXA = 0.2;            // TypeError

// careful: const prevents reassignment, not mutation
const lista = [1, 2];
lista.push(3);         // valid — the array can change
lista = [4];           // TypeError — the variable cannot

That last distinction confuses a lot of people. const freezes the binding between the name and the value, not the contents of the object. To make the object truly immutable, you need Object.freeze() or immutable data structures.

Scope: where the variable exists

  • Global scope — accessible from anywhere. Avoid it: it creates name collisions and coupling that is hard to trace.
  • Function scope — what var follows; the variable lives throughout the whole function, even when declared inside a block.
  • Block scope — what let and const follow; the variable exists only between the braces where it was declared.
The practical difference between var and let
for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 0);
}
// prints 3, 3, 3 — they all share the same variable

for (let j = 0; j < 3; j++) {
  setTimeout(() => console.log(j), 0);
}
// prints 0, 1, 2 — each iteration gets its own j

Typing: static or dynamic

In dynamically typed languages like JavaScript and Python, a variable declares no type and can receive values of different types over the course of execution. In statically typed languages like Java, Go and TypeScript, the type is declared or inferred and checked before the program runs — which turns runtime errors into compile-time errors.

Naming best practices

Prefer names that describe the content, not the type: usuariosAtivos communicates more than arr. Use const by default and switch to let only when reassignment is genuinely necessary — that signals intent to the reader. And avoid abbreviations that only make sense to whoever wrote them. The topic connects directly to programming logic and data structures.

## faq

Frequently asked questions

Should I use let or const?

Use const by default. If the linter or the compiler complains that the variable needs to be reassigned, switch to let. That makes it explicit which values change during execution and removes a whole class of accidental-modification bugs.

Why not use var?

Because its function scoping produces counterintuitive behavior, especially inside loops and conditional blocks. It also allows silently redeclaring the same name. let and const, introduced in ES6, fix both problems.

What is the difference between a variable and a constant?

A variable allows its value to change during execution; a constant cannot be reassigned once defined. In JavaScript, const creates a binding constant — the name cannot point to another value, although objects and arrays assigned to it can still be modified internally.