diff --git a/1-js/02-first-steps/07-operators/article.md b/1-js/02-first-steps/07-operators/article.md index 74b27e871..ab5f195c5 100644 --- a/1-js/02-first-steps/07-operators/article.md +++ b/1-js/02-first-steps/07-operators/article.md @@ -1,15 +1,15 @@ -# Operators +# Operadores -We know many operators from school. They are things like addition `+`, multiplication `*`, subtraction `-`, and so on. +Nós aprendemos sobre muitos operadores na escola. Alguns deles são a adição `+`,a multiplicação `*`, a subtração `-` e assim por diante. -In this chapter, we'll concentrate on aspects of operators that are not covered by school arithmetic. +Neste capítulo, iremos nos concentrar em aspectos dos operadores que não são cobertos pela aritmética escolar. -## Terms: "unary", "binary", "operand" +## Termos: "unário", "binário", "operandos" -Before we move on, let's grasp some common terminology. +Antes, vamos compreender alguns termos. -- *An operand* -- is what operators are applied to. For instance, in the multiplication of `5 * 2` there are two operands: the left operand is `5` and the right operand is `2`. Sometimes, people call these "arguments" instead of "operands". -- An operator is *unary* if it has a single operand. For example, the unary negation `-` reverses the sign of a number: +- *Um operando* -- é um valor onde os operadores são aplicados. Por exemplo, na multiplicacao `5 * 2` há dois operandos: o operadando da esquerda é `5` e o operando da direita é `2`. Às vezes, as pessoas os chamam de "argumentos" ao invés de "operandos". +- Um operador é *unário* se ele tem um único operando. Por exemplo, o unário negativo `-` reverte o sinal de um numero: ```js run let x = 1; @@ -17,67 +17,68 @@ Before we move on, let's grasp some common terminology. *!* x = -x; */!* - alert( x ); // -1, unary negation was applied + alert( x``` + ); // -1, o unário negativo foi aplicado ``` -- An operator is *binary* if it has two operands. The same minus exists in binary form as well: +- Um operador é *binário* se ele tem dois operandos. O mesmo menos existe na forma binária também: ```js run no-beautify let x = 1, y = 3; - alert( y - x ); // 2, binary minus subtracts values + alert( y - x ); // 2, o binário menos subtrai valores ``` - Formally, we're talking about two different operators here: the unary negation (single operand: reverses the sign) and the binary subtraction (two operands: subtracts). + Formalmente, nós estamos falando sobre dois operadores diferentes aqui: o unário negativo (único operando: reverte o sinal) e o binário negativo (dois operandos: subtrai). -## String concatenation, binary + +## Concatenação de String, binário + -Now, let's see special features of JavaScript operators that are beyond school arithmetics. +Agora, vamos ver características especiais dos operadores de JavaScript que vão além da aritmética escolar. -Usually, the plus operator `+` sums numbers. +Normalmente, o operador mais `+` soma números. -But, if the binary `+` is applied to strings, it merges (concatenates) them: +Mas, se o binário `+` é aplicado a strings, ele junta (concatena) elas: ```js -let s = "my" + "string"; -alert(s); // mystring +let s = "minha" + "string"; +alert(s); // minhastring ``` -Note that if one of the operands is a string, the other one is converted to a string too. +Note que se um dos operandos é uma string, o outro é convertido para uma string também. -For example: +Por exemplo: ```js run alert( '1' + 2 ); // "12" alert( 2 + '1' ); // "21" ``` -See, it doesn't matter whether the first operand is a string or the second one. The rule is simple: if either operand is a string, the other one is converted into a string as well. +Veja, não importa se o primeiro operando ou o segundo é uma string. A regra é simples: se um operando é uma string, o outro é convertido para uma string também. -However, note that operations run from left to right. If there are two numbers followed by a string, the numbers will be added before being converted to a string: +Entretanto, note que as operações são executadas da esquerda para direita. Se há dois números seguidos por uma string, os número serão somados antes e então convertidos para uma string: ```js run -alert(2 + 2 + '1' ); // "41" and not "221" +alert(2 + 2 + '1' ); // "41" e não "221" ``` -String concatenation and conversion is a special feature of the binary plus `+`. Other arithmetic operators work only with numbers and always convert their operands to numbers. + Concatenação e conversão de String é uma característica especial do binário mais `+`. Outros operadores aritméticos funcionam apenas com números e sempre convertem seus operandos para números. -For instance, subtraction and division: +Por exemplo, subtração e divisão: ```js run alert( 2 - '1' ); // 1 alert( '6' / '2' ); // 3 ``` -## Numeric conversion, unary + +## Conversão numérica, unário + -The plus `+` exists in two forms: the binary form that we used above and the unary form. +O mais `+` existe em duas formas: a forma binária que usamos acima e a forma unária. -The unary plus or, in other words, the plus operator `+` applied to a single value, doesn't do anything to numbers. But if the operand is not a number, the unary plus converts it into a number. +O unário mais ou, em outras palavras, o operador mais `+` aplicado a um único valor, não faz nada para números. Mas se o operando não é um número, o unário mais converte-o em um número. -For example: +Por exemplo: ```js run -// No effect on numbers +// Sem efeito em números let x = 1; alert( +x ); // 1 @@ -85,45 +86,45 @@ let y = -2; alert( +y ); // -2 *!* -// Converts non-numbers +// Converte não números alert( +true ); // 1 alert( +"" ); // 0 */!* ``` -It actually does the same thing as `Number(...)`, but is shorter. +Na realidade faz a mesma coisa que `Number(...)`, porém é mais curto. -The need to convert strings to numbers arises very often. For example, if we are getting values from HTML form fields, they are usually strings. +A necessidade de converter strings em números surge frequentemente. Por exemplo, se nós estamos pegando valores de campos de um formulário HTML, eles são geralmente strings. -What if we want to sum them? +E se nós quisermos somá-los? -The binary plus would add them as strings: +O binário mais deve somá-los como strings: ```js run let apples = "2"; let oranges = "3"; -alert( apples + oranges ); // "23", the binary plus concatenates strings +alert( apples + oranges ); // "23", o binário mais concatena strings ``` -If we want to treat them as numbers, we need to convert and then sum them: +E se nós quisermos tratá-los como números, nós precisamos convertê-los e então somá-los: ```js run let apples = "2"; let oranges = "3"; *!* -// both values converted to numbers before the binary plus +// ambos valores convertidos para números antes do binário mais alert( +apples + +oranges ); // 5 */!* -// the longer variant +// uma outra forma mais longa // alert( Number(apples) + Number(oranges) ); // 5 ``` -From a mathematician's standpoint, the abundance of pluses may seem strange. But from a programmer's standpoint, there's nothing special: unary pluses are applied first, they convert strings to numbers, and then the binary plus sums them up. +De um ponto de vista matemático, a abundância do operador mais pode parecer estranha. Mas de um ponto de vista de programadores, não há nada em especial: unário mais são aplicados primeiros, eles converetem strings para números, e então o mais binários soma-os. -Why are unary pluses applied to values before the binary ones? As we're going to see, that's because of their *higher precedence*. +Por que os mais unários são aplicados nos valores antes dos binários mais ? Como nós vamos ver, isto é por causa da sua *maior precedência*. ## Operator precedence diff --git a/figures.sketch b/figures.sketch index c740ac577..a50292b70 100644 Binary files a/figures.sketch and b/figures.sketch differ