|
1 |
| -# Variables |
| 1 | +# Variáveis |
2 | 2 |
|
3 |
| -Most of the time, a JavaScript application needs to work with information. Here are two examples: |
4 |
| -1. An online shop -- the information might include goods being sold and a shopping cart. |
5 |
| -2. A chat application -- the information might include users, messages, and much more. |
| 3 | +Na maioria das vezes, um aplicativo JavaScript precisa trabalhar com informações. Aqui estão dois exemplos: |
| 4 | +1. Uma loja online -- a informação pode incluir mercadorias vendidas e um carrinho de compras. |
| 5 | +2. Uma aplicação de chat -- a informação pode incluir usuários, mensagens e muito mais. |
6 | 6 |
|
7 |
| -Variables are used to store this information. |
| 7 | +Variáveis são usadas para armazenar esta informação. |
8 | 8 |
|
9 |
| -## A variable |
| 9 | +## Uma variável |
10 | 10 |
|
11 |
| -A [variable](https://en.wikipedia.org/wiki/Variable_(computer_science)) is a "named storage" for data. We can use variables to store goodies, visitors, and other data. |
| 11 | +Uma [variável](https://pt.wikipedia.org/wiki/Variável_(programação)) é um "armazenamento nomeado" para dados. Podemos usar variáveis para armazenar artigos, visitantes e outros dados. |
12 | 12 |
|
13 |
| -To create a variable in JavaScript, use the `let` keyword. |
| 13 | +Para criar uma variável em JavaScript, use a palavra-chave `let`. |
14 | 14 |
|
15 |
| -The statement below creates (in other words: *declares* or *defines*) a variable with the name "message": |
| 15 | +A declaração abaixo cria (em outras palavras: * declara * ou * define *) uma variável com o nome "message": |
16 | 16 |
|
17 | 17 | ```js
|
18 | 18 | let message;
|
19 | 19 | ```
|
20 | 20 |
|
21 |
| -Now, we can put some data into it by using the assignment operator `=`: |
| 21 | +Agora, podemos colocar alguns dados usando o operador de atribuição `=`: |
22 | 22 |
|
23 | 23 | ```js
|
24 | 24 | let message;
|
25 | 25 |
|
26 | 26 | *!*
|
27 |
| -message = 'Hello'; // store the string |
| 27 | +message = 'Olá'; // armazenar a string |
28 | 28 | */!*
|
29 | 29 | ```
|
30 | 30 |
|
31 |
| -The string is now saved into the memory area associated with the variable. We can access it using the variable name: |
| 31 | +A string agora é armazenada na área de memória associada à variável. Nós podemos acessá-la usando o nome da variável: |
32 | 32 |
|
33 | 33 | ```js run
|
34 | 34 | let message;
|
35 |
| -message = 'Hello!'; |
| 35 | +message = 'Olá!'; |
36 | 36 |
|
37 | 37 | *!*
|
38 |
| -alert(message); // shows the variable content |
| 38 | +alert(message); // mostra o conteúdo variável |
39 | 39 | */!*
|
40 | 40 | ```
|
41 | 41 |
|
42 |
| -To be concise, we can combine the variable declaration and assignment into a single line: |
| 42 | +Para ser conciso, podemos combinar a declaração de variável e atribuição em uma única linha: |
43 | 43 |
|
44 | 44 | ```js run
|
45 |
| -let message = 'Hello!'; // define the variable and assign the value |
| 45 | +let message = 'Olá!'; // define a variável e atribui o valor |
46 | 46 |
|
47 |
| -alert(message); // Hello! |
| 47 | +alert(message); // Olá! |
48 | 48 | ```
|
49 | 49 |
|
50 |
| -We can also declare multiple variables in one line: |
| 50 | +Podemos também declarar múltiplas variáveis em uma linha: |
51 | 51 |
|
52 | 52 | ```js no-beautify
|
53 |
| -let user = 'John', age = 25, message = 'Hello'; |
| 53 | +let user = 'John', age = 25, message = 'Olá'; |
54 | 54 | ```
|
55 | 55 |
|
56 |
| -That might seem shorter, but we don't recommend it. For the sake of better readability, please use a single line per variable. |
| 56 | +Isso pode parecer mais curto, mas não o recomendamos. Por uma questão de melhor legibilidade, use uma única linha por variável. |
57 | 57 |
|
58 |
| -The multiline variant is a bit longer, but easier to read: |
| 58 | +A variante multilinha é um pouco mais longa, mas mais fácil de ler: |
59 | 59 |
|
60 | 60 | ```js
|
61 | 61 | let user = 'John';
|
62 | 62 | let age = 25;
|
63 |
| -let message = 'Hello'; |
| 63 | +let message = 'Olá'; |
64 | 64 | ```
|
65 | 65 |
|
66 |
| -Some people also define multiple variables in this multiline style: |
| 66 | +Algumas pessoas também definem múltiplas variáveis nesse estilo multilinha: |
67 | 67 | ```js no-beautify
|
68 | 68 | let user = 'John',
|
69 | 69 | age = 25,
|
70 |
| - message = 'Hello'; |
| 70 | + message = 'Olá'; |
71 | 71 | ```
|
72 | 72 |
|
73 |
| -...Or even in the "comma-first" style: |
| 73 | +... Ou até mesmo no estilo "comma-first": |
74 | 74 |
|
75 | 75 | ```js no-beautify
|
76 | 76 | let user = 'John'
|
77 | 77 | , age = 25
|
78 |
| - , message = 'Hello'; |
| 78 | + , message = 'Olá'; |
79 | 79 | ```
|
80 | 80 |
|
81 |
| -Technically, all these variants do the same thing. So, it's a matter of personal taste and aesthetics. |
| 81 | +Tecnicamente, todas estas variantes fazem a mesma coisa. Então, é uma questão de gosto pessoal e estética. |
82 | 82 |
|
83 | 83 |
|
84 | 84 | ````smart header="`var` instead of `let`"
|
85 |
| -In older scripts, you may also find another keyword: `var` instead of `let`: |
| 85 | +Em scripts antigos, você também pode encontrar outra palavra-chave: `var` em vez de `let`: |
86 | 86 |
|
87 | 87 | ```js
|
88 |
| -*!*var*/!* message = 'Hello'; |
| 88 | +*!*var*/!* message = 'Olá'; |
89 | 89 | ```
|
90 | 90 |
|
91 |
| -The `var` keyword is *almost* the same as `let`. It also declares a variable, but in a slightly different, "old-school" way. |
| 91 | +A palavra-chave `var` é quase a mesma que `let`. Ela também declara uma variável, mas de um modo um pouco diferente, "old-school". |
92 | 92 |
|
93 |
| -There are subtle differences between `let` and `var`, but they do not matter for us yet. We'll cover them in detail in the chapter <info:var>. |
| 93 | +Existem diferenças sutis entre `let` e `var`, mas elas ainda não são importantes para nós. Nós as abordaremos em detalhes no capítulo <info:var>. |
94 | 94 | ````
|
95 | 95 |
|
96 | 96 | ## A real-life analogy
|
|
0 commit comments