★ wanayoo — archive 1999 https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Operators/Assignment_OperatorsNouvelle recherche | Portail wanayoo

Check out Firefox Developer Edition, the first browser built for developers like you. http://mzl.la/112VrtT

mozilla
Your Search Results

    Операторы присваивания

    This translation is incomplete. Please help translate this article from English.

    Сводка

    Оператор присваивания присваивает левому операнду значение правого операнда.

    Описание

    Простейший оператор присваивания (=) задаёт значение правого операнда левому. То есть, x = y присваивает значение переменной y переменной x. Другие операторы присваивания, как следует из приведенной ниже таблицы с определениями и примерами, являются сокращениями стандартных операций.

    Имя Сокращенный оператор Смысл
    Присваивание x = y x = y
    Присваивание со сложением x += y x = x + y
    Присваивание с вычитанием x -= y x = x - y
    Присваивание с умножением x *= y x = x * y
    Присваивание с делением x /= y x = x / y
    Присваивание по модулю x %= y x = x % y
    Присваивание с левым сдвигом x <<= y x = x << y
    Присваивание с правым сдвигом x >>= y x = x >> y
    Присваивание с беззнаковым сдвигом вправо x >>>= y x = x >>> y
    Присваивание с побитовым AND x &= y x = x & y
    Присваивание с побитовым XOR x ^= y x = x ^ y
    Присваивание с побитовым OR x |= y x = x | y

    Присваивание

    Simple assignment operator which assigns a value to a variable. Chaining the assignment operator is possible in order to assign a single value to multiple variables. See the example.

    Синтаксис

    Оператор: x = y
    

    Примеры

    // Assuming the following variables
    //  x = 5
    //  y = 10
    //  z = 25
    
    x = y     // x is 10
    x = y = z // x, y and z are all 25
    

    Присваивание со сложением

    The addition assignment operator adds the value of the right operand to a variable and assigns the result to the variable. The types of the two operands determine the behaviour of the addition assignment operator. Addition or concatenation is possible. See the addition operator for more details.

    Синтаксис

    Оператор: x += y 
    Значение:  x  = x + y
    

    Примеры

    // 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 // 0
    
    // Number + String -> concatenation
    bar += "foo" // "5foo"
    
    // String + Boolean -> concatenation
    foo += false // "foofalse"
    
    // String + String -> concatenation
    foo += "bar" // "foobar"
    

    Присваивание с вычитанием

    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.

    Синтаксис

    Оператор: x -= y 
    Значение:  x  = x - y
    

    Примеры

    // Assuming the following variable
    //  bar = 5
    
    bar -= 2     // 3
    bar -= "foo" // NaN
    

    Присваивание с умножением

    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.

    Синтаксис

    Оператор: x *= y 
    Значение:  x  = x * y
    

    Примеры

    // Assuming the following variable
    //  bar = 5
    
    bar *= 2     // 10
    bar *= "foo" // NaN
    

    Присваивание с делением

    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.

    Синтаксис

    Оператор: x /= y 
    Значение:  x  = x / y
    

    Примеры

    // Assuming the following variable
    //  bar = 5
    
    bar /= 2     // 2.5
    bar /= "foo" // NaN
    bar /= 0     // Infinity
    

    Присваивание по модулю

    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.

    Синтаксис

    Оператор: x %= y 
    Значение:  x  = x % y
    

    Примеры

    // Assuming the following variable
    // bar = 5
    
    bar %= 2     // 1
    bar %= "foo" // NaN
    bar %= 0     // NaN
    

    Присваивание с левым сдвигом

    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.

    Синтаксис

    Оператор: x <<= y 
    Значение:  x   = x << y
    

    Примеры

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

    Присваивание с правым сдвигом

    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.

    Синтаксис

    Оператор: x >>= y 
    Значение:  x   = x >> y
    

    Примеры

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

    Присваивание с беззнаковым сдвигом вправо

    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.

    Синтаксис

    Оператор: x >>>= y 
    Значение:  x    = x >>> y
    

    Примеры

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

    Присваивание с побитовым 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.

    Синтаксис

    Оператор: x &= y 
    Значение:  x  = x & y
    

    Примеры

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

    Присваивание с побитовым 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.

    Синтаксис

    Оператор: x ^= y 
    Значение:  x  = x ^ y
    

    Примеры

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

    Присваиванием с побитовым 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.

    Синтаксис

    Оператор: x |= y 
    Значение:  x  = x | y
    

    Примеры

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

    Примеры

    Левый операнд с другим оператором присваивания

    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
    

    Спецификации

    Спецификация Статус Комментарий
    ECMAScript 1-е издание. Стандарт Изначальное определение.
    ECMAScript 5.1 (ECMA-262)
    Определение 'Операторы присваивания' в этой спецификации.
    Стандарт  
    ECMAScript 6 (ECMA-262)
    Определение 'Операторы присваивания' в этой спецификации.
    Черновик  

    Совместимость с браузерами

    Возможность Chrome Firefox (Gecko) Internet Explorer Opera Safari
    Базовая поддержка (Да) (Да) (Да) (Да) (Да)
    Возможность Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
    Базовая поддержка (Да) (Да) (Да) (Да) (Да) (Да)

    Смотрите также

    Метки документа и участники

    На данную страницу внесли свой вклад: BurkovBA, dtretyakov, SphinxKnight
    Обновлялась последний раз: dtretyakov,
    Скрыть боковую панель