Mozilla's getting a new look. What do you think? https://mzl.la/brandsurvey

Assignment operators

Traducción en curso

Un operador de asignacion asigna un valor al operando de la izquierda basado en el valor del operando de la derecha.

Visión de conjunto

El operador de asignación basico es el igual (=), el cual asigna el valor del operando derecho al operando izquierdo. Es decir, x = y asigna el valor de y a x. El otro operador de asignacion son usualmente son abreviaciones para operaciones estandar, como se muestra en las siguientes definiciones y ejemplos.

Nombres Abreviaciones Significado
Asignación x = y x = y
Asignación de adicción x += y x = x + y
Asignación de sustracción x -= y x = x - y
Asignación de multiplicación x *= y x = x * y
Asignación de división x /= y x = x / y
Asignación de Resto x %= y x = x % y
Asignación de exponente x **= y x = x ** y
Asignación de desplazamiento a la izquierda x <<= y x = x << y
Asignación de desplazamiento a la derecha x >>= y x = x >> y
Asignación sin signo de desplazamiento a la derecha x >>>= y x = x >>> y
Asignacion AND x &= y x = x & y
Asignacion XOR x ^= y x = x ^ y
Asignacion OR x |= y x = x | y

Asignación

Operador de asignación simple que asigna un valor a una variable. EL operador de asignación evalua al valor asignado. Encadenando el operador de asignación es posible en orden de asignar un solo valor a multiples variables. Vea el ejemplo.

Sintaxis

Operador: x = y

Ejemplos

// Asumiendo las siguientes variables
//  x = 5
//  y = 10
//  z = 25

x = y     // x es 10
x = y = z // x, y, z son todas 25

Asignación de Adicción

El operador de asignación de suma agrega el valor del operando derecho a la variable y le asigna el resultado a la variable.

. The types of the two operands determine the behavior of the addition assignment operator. Addition or concatenation is possible. See the addition operator for more details.

Syntax

Operator: x += y 
Meaning:  x  = x + y

Examples

// Assuming the following variables
//  foo = "foo"
//  bar = 5
//  baz = true


// Number + Number -> addition
bar += 2 // 7

// Boolean + Number -> addition
baz += 1 // 2

// Boolean + Boolean -> addition
baz += false // 1

// Number + String -> concatenation
bar += "foo" // "5foo"

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

// String + String -> concatenation
foo += "bar" // "foobar"

Subtraction assignment

The subtraction assignment operator subtracts the value of the right operand from a variable and assigns the result to the variable. See the subtraction operator for more details.

Syntax

Operator: x -= y 
Meaning:  x  = x - y

Examples

// Assuming the following variable
//  bar = 5

bar -= 2     // 3
bar -= "foo" // NaN

Multiplication assignment

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

Examples

// Assuming the following variable
//  bar = 5

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

Division assignment

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

Examples

// Assuming the following variable
//  bar = 5

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

Remainder assignment

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

Examples

// Assuming the following variable
//  bar = 5

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

Exponentiation assignment

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 evaluates to 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

Examples

// Assuming the following variable
//  bar = 5

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

Left shift assignment

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

Examples

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

Right shift assignment

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

Examples

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

Examples

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

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

Bitwise AND assignment

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

Example

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

Bitwise XOR assignment

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

Example

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

Bitwise OR assignment

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

Example

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

Examples

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 is evaluated only once
a[i++] = a[i++] + 5 // i is evaluated twice

Specifications

Specification Status Comment
ECMAScript 2017 Draft (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.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

See also

Etiquetas y colaboradores del documento

 Colaboradores en esta página: maxbfmv55, maxbfmv
 Última actualización por: maxbfmv55,