Variables — let and const · Variáveis — let e const
A variable is a named box
- A variable stores a value and gives it a name, so you can use it again later.
letmakes a box you can change.constmakes a box that must stay the same (a constant).
Uma variável é uma caixa nomeada
- Uma variável armazena um valor e lhe dá um nome, para poder usá-la novamente depois.
letcria uma caixa que você pode alterar.constcria uma caixa que deve permanecer a mesma (uma constante).
let age = 15; // a value that may change
const name = "Ada"; // a value that won't
console.log(name, age); // → Ada 15
Change a let with a new value
- Assign again —
count = count + 1— to update alet. count++is a handy shortcut for "add 1 tocount".
Altere um let com um novo valor
- Atribua novamente —
count = count + 1— para atualizar umlet. count++é um atalho útil para "adicionar 1 acount".
let count = 0;
count = count + 1;
count++;
console.log(count); // → 2
Watch the boxes change · Observe as caixas mudarem
A variable is a named box. let can be changed; const stays fixed. · Uma variável é uma caixa com nome. let pode ser alterada; const permanece fixa.
Watch out
- A
constcannot be reassigned:const x = 1; x = 2;throws an error. - Good habit: use
constby default, and reach forletonly when the value truly changes.
Atenção
- Um
constnão pode ser reatribuído:const x = 1; x = 2;gera um erro. - Bom hábito: use
constpor padrão, e recorra aletapenas quando o valor realmente mudar.
Common mistakes
- Use
constfor values that never change,letfor those that do. - Declare a variable before you use it.
Erros comuns
- Use
constpara valores que nunca mudam,letpara aqueles que mudam. - Declare uma variável antes de usá-la.
Now you try
- Use
letorconstto store values. - Press Check answer to test it.
Agora você tenta
- Use
letouconstpara armazenar valores. - Pressione Check answer para testá-lo.
Store the text Cairo in a const called · chamado city, then log city. · Armazene o texto Cairo em um const chamado city, depois registre city.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
let w = 4 and · e let h = 3 are the width and height. Work out the area (w * h) and log it (it is 12). · let w = 4 e let h = 3 são a largura e altura. Calcule a área (w * h) e faça log dela (é 12).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Make a let total = 10, add 5 to it, then log total (it is 15). · Crie um let total = 10, adicione 5 a ele, depois faça log de total (é 15).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.