JavaScript opgave


Indholdsfortegnelse

    Vis indholdsfortegnelse


JavaScript-tildelingsoperatører

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

Skift tildelingsoperatører

Operator Example Same As
<<= x <<= y x = x << y
>>= x >>= y x = x >> y
>>>= x >>>= y x = x >>> y

Bitwise Assignment Operators

Operator Example Same As
&= x &= y x = x & y
^= x ^= y x = x ^ y
|= x |= y x = x | y

Logiske tildelingsoperatører

Operator Example Same As
&&= x &&= y x = x && (x = y)
||= x ||= y x = x || (x = y)
??= x ??= y x = x ?? (x = y)

Bemærk

De logiske tildelingsoperatører er ES2020


Den=Operatør

Simple Assignment Operator tildeler en værdi til en variabel.

Simple opgaveeksempler

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>

+=-operatøren

Addition Assignment Operator tilføjer en værdi til en variabel.

Eksempler på tilføjelsesopgave

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>

-= Operatøren

Subtraktionstildelingsoperatøren trækker en værdi fra en variabel.

Subtraktionsopgave eksempel

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>

*= Operatøren

Multiplication Assignment Operator multiplicerer en variabel.

Eksempel på multiplikationsopgave

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>

**= Operatøren

Exponentiation Assignment Operator hæver en variabel til operandens potens.

Eksponentieringstildelingseksempel

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>

/= Operatøren

Division Assignment Operator deler en variabel.

Eksempel på divisionsopgave

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>

%=-operatøren

Remainder Assignment Operator tildeler en rest til en variabel.

Eksempel på resterende opgave

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>


<<= Operatøren

Venstre Skift Assignment Operator til venstre flytter en variabel.

Venstre Skift Tildeling Eksempel

let x = -100;
x <<= 5;

Prøv det selv →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Assignments</h1>
<h2>Left Shift Assignment</h2>
<h3>The &lt;&lt;= Operator</h3>

<p id="demo"></p>

<script>
let x = -100;
x <<= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>

</body>
</html>

>>= Operatøren

Right Shift Assignment Operator til højre flytter en variabel (med fortegn).

Eksempel på tildeling af højreskift

let x = -100;
x >>= 5;

Prøv det selv →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Assignments</h1>
<h2>Right Shift Assignment</h2>
<h3>The &gt;&gt;= Operator</h3>

<p id="demo"></p>

<script>
let x = -100;
x >>= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>

</body>
</html>

>>>= Operatøren

Usigned Right Shift Assignment Operator til højre flytter en variabel (usigned).

Usigneret Right Shift-tildelingseksempel

let x = -100;
x >>>= 5;

Prøv det selv →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Assignments</h1>
<h2>Right Shift Assignment</h2>
<h3>The &gt;&gt;&gt;= Operator</h3>

<p id="demo"></p>

<script>
let x = -100;
x >>>= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>

</body>
</html>

&= Operatøren

Bitwise AND Assignment Operator udfører en bitwise AND-operation på to operander og tildeler resultatet til variablen.

Bitvis OG opgaveeksempel

let x = 10;
x &= 5;

Prøv det selv →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Assignments</h1>
<h2>Bitwise AND Assignment</h2>
<h3>The &amp;= Operator</h3>

<p id="demo"></p>

<script>
let x = 100;
x &= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>

</body>
</html>

|= Operatøren

Bitwise OR Assignment Operator udfører en bitwise OR-operation på to operander og tildeler resultatet til variablen.

Bitwise OR Assignment Eksempel

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>

^= Operatøren

Bitwise XOR Assignment Operator udfører en bitwise XOR-operation på to operander og tildeler resultatet til variablen.

Bitwise XOR Assignment Eksempel

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>

&&=-operatøren

Operatoren Logical AND assignment bruges mellem to værdier.

Hvis den første værdi er sand, tildeles den anden værdi.

Logisk OG opgaveeksempel

let x = 10;
x &&= 5;

Prøv det selv →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Assignments</h1>
<h2>Logical AND Assignment</h2>
<h3>The &amp;&amp;= 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.


||=-operatøren

Logisk ELLER-tildelingsoperator bruges mellem to værdier.

Hvis den første værdi er falsk, tildeles den anden værdi.

Eksempel på logisk ELLER tildeling

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.


??= Operatøren

Operatoren Nullish coalescing assignment bruges mellem to værdier.

Hvis den første værdi er udefineret eller null, tildeles den anden værdi.

Nullish Coalescing Assignment Eksempel

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.