Tildelingsoperatorer tildeler værdier til JavaScript-variabler.
Operator | Example | Same As |
---|---|---|
= | 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 |
Operator | Example | Same As |
---|---|---|
<<= | x <<= y | x = x << y |
>>= | x >>= y | x = x >> y |
>>>= | x >>>= y | x = x >>> y |
Operator | Example | Same As |
---|---|---|
&= | x &= y | x = x & y |
^= | x ^= y | x = x ^ y |
|= | x |= y | x = x | y |
Operator | Example | Same As |
---|---|---|
&&= | x &&= y | x = x && (x = y) |
||= | x ||= y | x = x || (x = y) |
??= | x ??= y | x = x ?? (x = y) |
De logiske tildelingsoperatører er ES2020
Simple Assignment Operator tildeler en værdi til en variabel.
let x = 10;
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Simple Assignment</h2>
<h3>The = Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
let x = 10 + y;
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Simple Assignment</h2>
<h3>The = Operator</h3>
<p id="demo"></p>
<script>
let y = 50
let x = 10 + y;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
Addition Assignment Operator tilføjer en værdi til en variabel.
let x = 10;
x += 5;
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Addition Assignment</h2>
<h3>The += Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
x += 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
let text = "Hello";
text += " World";
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Addition Assignment</h2>
<h3>The += Operator</h3>
<p id="demo"></p>
<script>
let text = "Hello";
text += " World";
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
Subtraktionstildelingsoperatøren trækker en værdi fra en variabel.
let x = 10;
x -= 5;
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Subtraction Assignment</h2>
<h3>The -= Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
x -= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
Multiplication Assignment Operator multiplicerer en variabel.
let x = 10;
x *= 5;
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Multiplication Assignment</h2>
<h3>The *= Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
x *= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
Exponentiation Assignment Operator hæver en variabel til operandens potens.
let x = 10;
x **= 5;
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Exponentiation Assignment</h2>
<h3>The **= Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
x **= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
Division Assignment Operator deler en variabel.
let x = 10;
x /= 5;
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Division Assignment</h2>
<h3>The /= Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
x /= 5;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
Remainder Assignment Operator tildeler en rest til en variabel.
let x = 10;
x %= 5;
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Remainder Assignment</h2>
<h3>The %= Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
x %= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
Venstre Skift Assignment Operator til venstre flytter en variabel.
let x = -100;
x <<= 5;
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Left Shift Assignment</h2>
<h3>The <<= Operator</h3>
<p id="demo"></p>
<script>
let x = -100;
x <<= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
Right Shift Assignment Operator til højre flytter en variabel (med fortegn).
let x = -100;
x >>= 5;
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Right Shift Assignment</h2>
<h3>The >>= Operator</h3>
<p id="demo"></p>
<script>
let x = -100;
x >>= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
Usigned Right Shift Assignment Operator til højre flytter en variabel (usigned).
let x = -100;
x >>>= 5;
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Right Shift Assignment</h2>
<h3>The >>>= Operator</h3>
<p id="demo"></p>
<script>
let x = -100;
x >>>= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
Bitwise AND Assignment Operator udfører en bitwise AND-operation på to operander og tildeler resultatet til variablen.
let x = 10;
x &= 5;
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Bitwise AND Assignment</h2>
<h3>The &= Operator</h3>
<p id="demo"></p>
<script>
let x = 100;
x &= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
Bitwise OR Assignment Operator udfører en bitwise OR-operation på to operander og tildeler resultatet til variablen.
let x = 10;
x |= 5;
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Bitwise OR Assignment</h2>
<h3>The |= Operator</h3>
<p id="demo"></p>
<script>
let x = 100;
x |= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
Bitwise XOR Assignment Operator udfører en bitwise XOR-operation på to operander og tildeler resultatet til variablen.
let x = 10;
x ^= 5;
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Bitwise XOR Assignment</h2>
<h3>The ^= Operator</h3>
<p id="demo"></p>
<script>
let x = 100;
x ^= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
Operatoren Logical AND assignment bruges mellem to værdier.
Hvis den første værdi er sand, tildeles den anden værdi.
let x = 10;
x &&= 5;
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Logical AND Assignment</h2>
<h3>The &&= Operator</h3>
<p>If the first value is true, the second value is assigned.</p>
<p id="demo"></p>
<script>
let x = 100;
x &&= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
Operatøren &&=
er en ES2020-funktion.
Logisk ELLER-tildelingsoperator bruges mellem to værdier.
Hvis den første værdi er falsk, tildeles den anden værdi.
let x = 10;
x ||= 5;
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Logical OR Assignment</h2>
<h3>The ||= Operator</h3>
<p>If the first value is false, the second value is assigned:</p>
<p id="demo"></p>
<script>
let x = undefined;
x ||= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
Operatoren ||=
er en ES2020-funktion.
Operatoren Nullish coalescing assignment bruges mellem to værdier.
Hvis den første værdi er udefineret eller null, tildeles den anden værdi.
let x;
x ??= 5;
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>The ??= Operator</h2>
<p>The ??= operator is used between two values. If the first value is undefined or null, the second value is assigned.</p>
<p id="demo"></p>
<script>
let x;
document.getElementById("demo").innerHTML = x ??= 5;
</script>
</body>
</html>
Operatoren ??=
er en ES2020-funktion.