Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Operadores de Atribuição

Este artigo necessita de uma revisão editorial. Como posso ajudar.

Esta tradução está incompleta. Ajude atraduzir este artigo.

Um operador de atribuição, como o próprio nome sugere, atribui um valor para uma variável, objeto, etc.

Visão geral

O operador de atribuição básico é o "igual" (=), que atribui um valor da direita para o operando (variável) que está a sua esquerda. Dessa forma: x = y está ocorrendo a atribuição do valor de y para x. Os operadores de atribuição são geralmente utilizados em operações padrões como mostrado nos exemplos a seguir.

Nome Shorthand operator Significado
Atribuição x = y x = y
Atribuição com adição x += y x = x + y
Atribuição com subtração x -= y x = x - y
Atribuição com multiplicação x *= y x = x * y
Atribuição com divisão x /= y x = x / y
Atribuição com resto x %= y x = x % y
Atribuição de exponenciação x **= y x = x ** y
Left shift assignment x <<= y x = x << y
Right shift assignment x >>= y x = x >> y
Unsigned right shift assignment x >>>= y x = x >>> y
Bitwise AND atribuição x &= y x = x & y
Bitwise XOR atribuição x ^= y x = x ^ y
Bitwise OR atribuição x |= y x = x | y

Assignment

É um operador de atribuição simples que atribui um valor a uma variável.  Encadeando o operador de atribuição é possível atribuir um único valor a várias variáveis, veja os exemplos:

Syntax

Operator: x = y

Exemplos

// Dadas as seguintes variáveis
//  x = 5
//  y = 10
//  z = 25

x = y     // agora x tem o valor 10
x = y = z // x, y e z estão com o valor 25

Operador de Adição

O operador de adição, adiciona um valor com o já existente na variável. Se for numérico irá somar, se string irá concatenar. Para mais detalhes: addition operator

Syntax

Operador: x += y 
O mesmo que:  x  = x + y

Exemplos

// Dadas as seguintes variáveis
//  foo = "foo"
//  bar = 5
//  baz = true


// Numero + Numero -> adição (soma os valores)
bar += 2 // O mesmo que: 7 = 5 + 2

// Boolean + Numero -> adição
baz += 1 // 2 (true = 1, false = 0)

// Boolean + Boolean -> adição
baz += false // 1

// Numero + String -> concatenação
bar += "foo" // "5foo"

// String + Boolean -> concatenation
foo += false // "foofalse"

// String + String -> concatenação
foo += "bar" // "foobar"

Operador de Subtração

O operador de subtração, como sugerido, realizar uma subtração quando valores numéricos. Para mais detalhes: subtraction operator 

Syntax

Operador: x -= y 
O mesmo que:  x  = x - y

Exemplos

// Dadas as variáveis
//  bar = 5

bar -= 2     // 3
bar -= "foo" // NaN (Não numérico)

Operador de Multiplicação

The multiplication assignment operator multiplies a variable by the value of the right operand and assigns the result to the variable. See the multiplication operator for more details.

Syntax

Operator: x *= y 
Meaning:  x  = x * y

Exemplos

// Assuming the following variable
//  bar = 5

bar *= 2     // 10
bar *= "foo" // NaN

Operador de Divisão

The division assignment operator divides a variable by the value of the right operand and assigns the result to the variable. See the division operator for more details.

Syntax

Operator: x /= y 
Meaning:  x  = x / y

Exemplos

// Assuming the following variable
//  bar = 5

bar /= 2     // 2.5
bar /= "foo" // NaN
bar /= 0     // Infinity

Operador de Resto ou Módulo

The remainder assignment operator divides a variable by the value of the right operand and assigns the remainder to the variable. See the remainder operator for more details.

Syntax

Operator: x %= y 
Meaning:  x  = x % y

Exemplo

// Assuming the following variable
//  bar = 5

bar %= 2     // 1
bar %= "foo" // NaN
bar %= 0     // NaN

Operador de Exponenciação

This is an experimental technology, part of the ECMAScript 2016 (ES7) proposal.
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.

The exponentiation assignment operator returns the result of raising first operand to the power second operand. See the exponentiation operator for more details.

Syntax

Operator: x **= y 
Meaning:  x  = x ** y

Exemplos

// Assuming the following variable
//  bar = 5

bar **= 2     // 25
bar **= "foo" // NaN

Operador Left shift

The left shift assignment operator moves the specified amount of bits to the left and assigns the result to the variable. See the left shift operator for more details.

Syntax

Operator: x <<= y 
Meaning:  x   = x << y

Exemplos

var bar = 5; //  (00000000000000000000000000000101)
bar <<= 2; // 20 (00000000000000000000000000010100)

Operador Right shift

The right shift assignment operator moves the specified amount of bits to the right and assigns the result to the variable. See the right shift operator for more details.

Syntax

Operator: x >>= y 
Meaning:  x   = x >> y

Exemplos

var bar = 5; //   (00000000000000000000000000000101)
bar >>= 2;   // 1 (00000000000000000000000000000001)

var bar -5; //    (-00000000000000000000000000000101)
bar >>= 2;  // -2 (-00000000000000000000000000000010)

Unsigned right shift assignment

The unsigned right shift assignment operator moves the specified amount of bits to the right and assigns the result to the variable. See the unsigned right shift operator for more details.

Syntax

Operator: x >>>= y 
Meaning:  x    = x >>> y

Exemplos

var bar = 5; //   (00000000000000000000000000000101)
bar >>>= 2;  // 1 (00000000000000000000000000000001)

var bar = -5; // (-00000000000000000000000000000101)
bar >>>= 2; // 1073741822 (00111111111111111111111111111110)

Operador Bitwise AND

The bitwise AND assignment operator uses the binary representation of both operands, does a bitwise AND operation on them and assigns the result to the variable. See the bitwise AND operator for more details.

Syntax

Operator: x &= y 
Meaning:  x  = x & y

Exemplo

var bar = 5;
// 5:     00000000000000000000000000000101
// 2:     00000000000000000000000000000010
bar &= 2; // 0

Operador Bitwise XOR

The bitwise XOR assignment operator uses the binary representation of both operands, does a bitwise XOR operation on them and assigns the result to the variable. See the bitwise XOR operator for more details.

Syntax

Operator: x ^= y 
Meaning:  x  = x ^ y

Exemplo

var bar = 5;
bar ^= 2; // 7
// 5: 00000000000000000000000000000101
// 2: 00000000000000000000000000000010
// -----------------------------------
// 7: 00000000000000000000000000000111

Operador Bitwise OR

The bitwise OR assignment operator uses the binary representation of both operands, does a bitwise OR operation on them and assigns the result to the variable. See the bitwise OR operator for more details.

Syntax

Operator: x |= y 
Meaning:  x  = x | y

Exemplo

var bar = 5;
bar |= 2; // 7
// 5: 00000000000000000000000000000101
// 2: 00000000000000000000000000000010
// -----------------------------------
// 7: 00000000000000000000000000000111

Exemplos

Left operand with another assignment operator

In unusual situations, the assignment operator (e.g. x += y) is not identical to the meaning expression (here x = x + y). When the left operand of an assignment operator itself contains an assignment operator, the left operand is evaluated only once. For example:

a[i++] += 5         // i calculado apenas uma vez
a[i++] = a[i++] + 5 // i calculado duas vezes

Specificações

Specificação Status Comment
ECMAScript 2016 Draft (7th Edition, ECMA-262)
The definition of 'Assignment operators' in that specification.
Draft  
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Assignment operators' in that specification.
Standard  
ECMAScript 5.1 (ECMA-262)
The definition of 'Assignment operators' in that specification.
Standard  
ECMAScript 1st Edition (ECMA-262)
The definition of 'Assignment operators' in that specification.
Standard Initial definition.

Compatibilidade entre Browsers

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Suporte básico (Yes) (Yes) (Yes) (Yes) (Yes)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Suporte básico (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

Veja também

Etiquetas do documento e colaboradores

 Colaboradores desta página: Gutemberg
 Última atualização por: Gutemberg,