GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — JavaScript - Assignment Operators
Selected Reading
JavaScript - Assignment OperatorsJavaScript Assignment OperatorsThe assignment operators in JavaScript are used to assign values to the variables. These are binary operators. An assignment operator takes two operands, assigns a value to the left operand based on the value of right operand. The left operand is always a variable and the right operand may be literal, variable or expression. let x = 10; // right operand is a literal let y = x; // right operand is a variable let z = x + 10; // right operand is an expression An assignment operator first evaluates the expression and then assign the value to the variable (left operand). A simple assignment operator is equal (=) operator. In the JavaScript statement "let x = 10;", the = operator assigns 10 to the variable x. We can combine a simple assignment operator with other type of operators such as arithmetic, logical, etc. to get compound assignment operators. Some arithmetic assignment operators are +=, -=, *=, /=, etc. The += operator performs addition operation on the operands and assign the result to the left hand operand. Arithmetic Assignment OperatorsIn this section, we will cover simple assignment and arithmetic assignment operators. An arithmetic assignment operator performs arithmetic operation and assign the result to a variable. Following is the list of operators with example −
Simple Assignment (=) OperatorA simple assignment (=) operator assigns a value to a variable. We can assign a single value to multiple variables. This is known as assignment chaining.<html>
<body>
<div id="output"></div>
<script>
let x = 5;
let y = x + 10;
document.getElementById("output").innerHTML =
"Value of x : " + x + "<br>" +
"Value of y : " + y;
</script>
</body>
</html>
Below is an example of assignment chaining − <html>
<body>
<div id="output"></div>
<script>
let x = y = 5;
document.getElementById("output").innerHTML =
"Value of x : " + x + "<br>" +
"Value of y : " + y;
</script>
</body>
</html>
Addition Assignment (+=) OperatorThe JavaScript addition assignment operator performs addition on the two operands and assigns the result to the left operand. Here addition may be numeric addition or string concatenation. x += b; In the above statement, it adds values of b and x and assigns the result to x. Example: Numeric Addition Assignment<html>
<body>
<div id="output"></div>
<script>
let x = 5;
x += 7;
document.getElementById("output").innerHTML = "Value of x : " + x;
</script>
</body>
</html>
Example: String Concatenation Assignment<html>
<body>
<div id="output"></div>
<script>
let x ="Hello";
x += " World";
document.getElementById("output").innerHTML = "Value of x : " + x;
</script>
</body>
</html>
Subtraction Assignment (-=) OperatorThe subtraction assignment operator in JavaScript subtracts the value of right operand from the left operand and assigns the result to left operand (variable). let x -=b; In the above statement, it subtracts b from x and assigns the result to x. <html>
<body>
<div id="output"></div>
<script>
let x = 15;
x -= 5;
document.getElementById("output").innerHTML = "Value of x : " + x;
</script>
</body>
</html>
Multiplication Assignment (*=) OperatorThe multiplication assignment operator in JavaScript multiplies the both operands and assign the result to the left operand. let x *= b; In the above statement, it multiplies x and b and assigns the result to x. <html>
<body>
<div id="output"></div>
<script>
let x = 10;
x *= 5;
document.getElementById("output").innerHTML = "Value of x : " + x;
</script>
</body>
</html>
Division Assignment (/=) OperatorThis operator divides left operand by the right operand and assigns the result to left operand. let x /= b; In the above statement, it divides x by b and assigns the result (quotient) to x. <html>
<body>
<div id="output"></div>
<script>
let x = 10;
x /= 5;
document.getElementById("output").innerHTML = "Value of x : " + x;
</script>
</body>
</html>
Remainder Assignment (%=) OperatorThe JavaScript remainder assignment operator performs the remainder operation on the operands and assigns the result to left operand. let x %= b; In the above statement, it divides x by b and assigns the result (remainder) to x. <html>
<body>
<div id="output"></div>
<script>
let x = 12;
x %= 5;
document.getElementById("output").innerHTML = "Value of x : " + x;
</script>
</body>
</html>
Exponentiation Assignment (**=) OperatorThis operator performs exponentiation operation on the operands and assigns the result to left operand. let x **= b; In the above statement, it computes x**b and assigns the result to x. <html>
<body>
<div id="output"></div>
<script>
let x = 5;
x **= 3;
document.getElementById("output").innerHTML = "Value of x : " + x;
</script>
</body>
</html>
JavaScript Bitwise Assignment operatorsA bitwise assignment operator performs bitwise operation on the operands and assign the result to a variable. These operations perform two operations, first a bitwise operation and second the simple assignment operation. Bitwise operation is done on bit-level. A bitwise operator treats both operands as 32-bit signed integers and perform the operation on corresponding bits of the operands. The simple assignment operator assigns result is to the variable (left operand). Following is the list of operators with example −
Bitwise AND Assignment OperatorThe JavaScript bitwise AND assignment (&=) operator performs bitwise AND operation on the operands and assigns the result to the left operand (variable). let x &= b; In the above statement, it performs bitwise AND on x and b and assigns the result to the variable x. <html>
<body>
<div id="output"></div>
<script>
let x = 7;
x &= 5;
document.getElementById("output").innerHTML = "Value of x : " + x;
</script>
</body>
</html>
Bitwise OR Assignment OperatorThe JavaScript bitwise OR assignment (|=) operator performs bitwise OR operation on the operands and assigns the result to the left operand (variable). let x |= b; In the above statement, it performs bitwise OR on x and b and assigns the result to the variable x. <html>
<body>
<div id="output"></div>
<script>
let x = 7;
x |= 5;
document.getElementById("output").innerHTML = "Value of x : " + x;
</script>
</body>
</html>
Bitwise XOR Assignment OperatorThe JavaScript bitwise XOR assignment (^=) operator performs bitwise XOR operation on the operands and assigns the result to the left operand (variable). let x ^= b; In the above statement, it performs bitwise XOR on x and b and assigns the result to the variable x. <html>
<body>
<div id="output"></div>
<script>
let x = 7;
x ^= 5;
document.getElementById("output").innerHTML = "Value of x : " + x;
</script>
</body>
</html>
JavaScript Shift Assignment OperatorsA shift assignment operator performs bitwise shift operation on the operands and assign the result to a variable (left operand). These are a combinations two operators, the first bitwise shift operator and second the simple assignment operator. Following is the list of the shift assignment operators with example −
Left Shift Assignment OperatorThe JavaScript left shift assignment (<<=) operator performs left shift operation on the operands and assigns the result to the left operand (variable). let x <<= b; In the above statement, it performs left shift on x and b and assigns the result to the variable x. <html>
<body>
<div id="output"></div>
<script>
let x = 7;
x <<= 2;
document.getElementById("output").innerHTML = "Value of x : " + x;
</script>
</body>
</html>
Right Shift Assignment OperatorThe JavaScript right shift assignment (>>=) operator performs right shift operation on the operands and assigns the result to the left operand (variable). let x >>= b; In the above statement, it performs right shift on x and b and assigns the result to the variable x. <html>
<body>
<div id="output"></div>
<script>
let x = 7;
x >>= 1;
document.getElementById("output").innerHTML = "Value of x : " + x;
</script>
</body>
</html>
Unsigned Right Shift Assignment OperatorThe JavaScript unsigned right shift assignment (>>>=) operator performs unsigned right shift operation on the operands and assigns the result to the left operand (variable). let x >>>= b; In the above statement, it performs unsigned right shift on x and b and assigns the result to the variable x. <html>
<body>
<div id="output"></div>
<script>
let x = 7;
x >>>= 2;
document.getElementById("output").innerHTML ="Value of x : " + x;
</script>
</body>
</html>
JavaScript Logical Assignment operatorsIn JavaScript, a logical assignment operator performs a logical operation on the operands and assign the result to a variable (left operand). Each logical assignment operator is a combinations two operators, the first logical operator and second the simple assignment operator. Following is the list of the logical assignment operators with example −
Example<html>
<body>
<div id="output"></div>
<script>
var a = 5;
var b = 10;
var result = (a &&= b);
document.getElementById("output").innerHTML =
"Value of (a &&= b) => " + result + "<br/>";
result = (a ||= b);
document.getElementById("output").innerHTML +=
"Value of (a ||= b) => " + result;
</script>
</body>
</html>
Advertisements
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |