Sammenlignings- og logiske operatorer bruges til at teste for sand
eller false
.
Sammenligningsoperatorer bruges i logiske udsagn til at bestemme lighed eller forskel mellem variabler eller værdier.
I betragtning af at x=5
forklarer tabellen nedenfor sammenligningsoperatorerne:
==
Beskrivelse: lig med
Sammenligner:
x == 8
Vender tilbage :
false
Prøv det →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Comparison</h1>
<h2>The == Operator</h2>
<p>Assign 5 to x, and display the value of the comparison (x == 8):</p>
<p id="demo"></p>
<script>
let x = 5;
document.getElementById("demo").innerHTML = (x == 8);
</script>
</body>
</html>
Sammenligner:
x == 5
Vender tilbage :
true
Prøv det →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Comparison</h1>
<h2>The == Operator</h2>
<p>Assign 5 to x, and display the value of the comparison (x == 5):</p>
<p id="demo"></p>
<script>
let x = 5;
document.getElementById("demo").innerHTML = (x == 5);
</script>
</body>
</html>
Sammenligner:
x == "5"
Vender tilbage :
true
Prøv det →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Comparison</h1>
<h2>The == Operator</h2>
<p>Assign 5 to x, and display the value of the comparison (x == 5):</p>
<p id="demo"></p>
<script>
let x = 5;
document.getElementById("demo").innerHTML = (x == "5");
</script>
</body>
</html>
===
Beskrivelse: samme værdi og samme type
Sammenligner:
x === 5
Vender tilbage :
true
Prøv det →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Comparison</h1>
<h2>The === Operator</h2>
<p>Assign 5 to x, and display the value of the comparison (x === 5):</p>
<p id="demo"></p>
<script>
let x = 5;
document.getElementById("demo").innerHTML = (x === 5);
</script>
</body>
</html>
Sammenligner:
x === "5"
Vender tilbage :
false
Prøv det →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Comparison</h1>
<h2>The === Operator</h2>
<p>Assign 5 to x, and display the value of the comparison (x === "5").</p>
<p id="demo"></p>
<script>
let x = 5;
document.getElementById("demo").innerHTML = (x === "5");
</script>
</body>
</html>
!=
Beskrivelse: ikke ens
Sammenligner:
x != 8
Vender tilbage :
true
Prøv det →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Comparison</h1>
<h2>The != Operator</h2>
<p>Assign 5 to x, and display the value of the comparison (x != 8).</p>
<p id="demo"></p>
<script>
let x = 5;
document.getElementById("demo").innerHTML = (x != 8);
</script>
</body>
</html>
!==
Beskrivelse: ikke samme værdi eller ikke samme type
Sammenligner:
x !== 5
Vender tilbage :
false
Prøv det →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Comparison</h1>
<h2>The !== Operator</h2>
<p>Assign 5 to x, and display the value of the comparison (x !== 5):</p>
<p id="demo"></p>
<script>
let x = 5;
document.getElementById("demo").innerHTML = (x !== 5);
</script>
</body>
</html>
Sammenligner:
x !== "5"
Vender tilbage :
true
Prøv det →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Comparison</h1>
<h2>The !== Operator</h2>
<p>Assign 5 to x, and display the value of the comparison (x !== "5"):</p>
<p id="demo"></p>
<script>
let x = 5;
document.getElementById("demo").innerHTML = (x !== "5");
</script>
</body>
</html>
Sammenligner:
x !== 8
Vender tilbage :
true
Prøv det →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Comparison</h1>
<h2>The !== Operator</h2>
<p>Assign 5 to x, and display the value of the comparison (x !== 8):</p>
<p id="demo"></p>
<script>
let x = 5;
document.getElementById("demo").innerHTML = (x !== 8);
</script>
</body>
</html>
>
Beskrivelse: større end
Sammenligner:
x > 8
Vender tilbage :
false
Prøv det →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Comparison</h1>
<h2>The > Operator</h2>
<p>Assign 5 to x, and display the value of the comparison (x > 8).</p>
<p id="demo"></p>
<script>
let x = 5;
document.getElementById("demo").innerHTML = (x > 8);
</script>
</body>
</html>
<
Beskrivelse: mindre end
Sammenligner:
x < 8
Vender tilbage :
true
Prøv det →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Comparison</h1>
<h2>The < Operator</h2>
<p>Assign 5 to x, and display the value of the comparison (x < 8).</p>
<p id="demo"></p>
<script>
let x = 5;
document.getElementById("demo").innerHTML = (x < 8);
</script>
</body>
</html>
>=
Beskrivelse: større end eller lig med
Sammenligner:
x >= 8
Vender tilbage :
false
Prøv det →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Comparison</h1>
<h2>The >= Operator</h2>
<p>Assign 5 to x, and display the value of the comparison (x >= 8).</p>
<p id="demo"></p>
<script>
let x = 5;
document.getElementById("demo").innerHTML = (x >= 8);
</script>
</body>
</html>
<=
Beskrivelse: mindre end eller lig med
Sammenligner:
x <= 8
Vender tilbage :
true
Prøv det →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Comparison</h1>
<h2>The <= Operator</h2>
<p>Assign 5 to x, and display the value of the comparison (x <= 8).</p>
<p id="demo"></p>
<script>
let x = 5;
document.getElementById("demo").innerHTML = (x <= 8);
</script>
</body>
</html>
Sammenligningsoperatorer kan bruges i betingede udsagn til at sammenligne værdier og tag handling afhængigt af resultatet:
if (age < 18) text = "Too young to buy alcohol";
Du vil lære mere om brugen af betingede udsagn i det næste kapitel i denne øvelse.
Logiske operatorer bruges til at bestemme logikken mellem variabler eller værdier.
I betragtning af at x=6
og y=3
, forklarer tabellen nedenfor de logiske operatorer:
&&
Beskrivelse: og
Sammenligner:
(x < 10 && y > 1)
Vender tilbage :
true
Prøv det →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Comparison</h1>
<h2>The && Operator (Logical AND)</h2>
<p>The && operator returns true if both expressions are true, otherwise it returns false.</p>
<p id="demo"></p>
<script>
let x = 6;
let y = 3;
document.getElementById("demo").innerHTML =
(x < 10 && y > 1) + "<br>" +
(x < 10 && y < 1);
</script>
</body>
</html>
||
Beskrivelse: eller
Sammenligner:
(x == 5 || y == 5)
Vender tilbage :
false
Prøv det →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Comparison</h1>
<h2>The || Operator (Logical OR)</h2>
<p>The || returns true if one or both expressions are true, otherwise it returns false.</p>
<p id="demo"></p>
<script>
let x = 6;
let y = 3;
document.getElementById("demo").innerHTML =
(x == 5 || y == 5) + "<br>" +
(x == 6 || y == 0) + "<br>" +
(x == 0 || y == 3) + "<br>" +
(x == 6 || y == 3);
</script>
</body>
</html>
!
Beskrivelse: ikke
Sammenligner:
!(x == y)
Vender tilbage :
true
Prøv det →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Comparison</h2>
<p>The NOT operator (!) returns true for false statements and false for true statements.</p>
<p id="demo"></p>
<script>
let x = 6;
let y = 3;
document.getElementById("demo").innerHTML =
!(x === y) + "<br>" +
!(x > y);
</script>
</body>
</html>
JavaScript indeholder også en betinget operator, der tildeler en værdi til en variabel baseret på en betingelse.
variablename = (condition) ? value1:value2
let voteable = (age < 18) ? "Too young":"Old enough";
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Comparison</h1>
<h2>The () ? : Ternary Operator</h2>
<p>Input your age and click the button:</p>
<input id="age" value="18" />
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
let age = document.getElementById("age").value;
let voteable = (age < 18) ? "Too young":"Old enough";
document.getElementById("demo").innerHTML = voteable + " to vote.";
}
</script>
</body>
</html>
Hvis den variable alder er en værdi under 18, kan værdien af variablen stemmes vil være "For ung", ellers vil værdien af stemmeberettiget være "Gammel nok".
Sammenligning af data af forskellige typer kan give uventede resultater.
Når man sammenligner en streng med et tal, vil JavaScript konvertere strengen til et tal, når du laver sammenligningen. En tom streng konverteres til 0. En ikke-numerisk streng konverterer til NaN
, som altid er falsk
.
2 < 12
Værdi:
true
Prøv det →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Comparison</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 2 < 12;
</script>
</body>
</html>
2 < "12"
Værdi:
true
Prøv det →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Comparison</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 2 < "12";
</script>
</body>
</html>
2 < "John"
Værdi:
false
Prøv det →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Comparison</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 2 < "John";
</script>
</body>
</html>
2 > "John"
Værdi:
false
Prøv det →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Comparison</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 2 > "John";
</script>
</body>
</html>
2 == "John"
Værdi:
false
Prøv det →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Comparison</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 2 == "John";
</script>
</body>
</html>
"2" < "12"
Værdi:
false
Prøv det →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Comparison</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "2" < "12";
</script>
</body>
</html>
"2" > "12"
Værdi:
true
Prøv det →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Comparison</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "2" > "12";
</script>
</body>
</html>
"2" == "12"
Værdi:
false
Prøv det →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Comparison</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "2" == "12";
</script>
</body>
</html>
Når man sammenligner to strenge, vil "2" være større end "12", fordi (alfabetisk) 1 er mindre end 2.
For at sikre et korrekt resultat, bør variabler konverteres til den rigtige type før sammenligning:
age = Number(age);
if (isNaN(age)) {
voteable = "Input is not a number";
} else {
voteable = (age < 18) ? "Too young" : "Old enough";
}
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Comparisons</h2>
<p>Input your age and click the button:</p>
<input id="age" value="18" />
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
let voteable;
let age = Number(document.getElementById("age").value);
if (isNaN(age)) {
voteable = "Input is not a number";
} else {
voteable = (age < 18) ? "Too young" : "Old enough";
}
document.getElementById("demo").innerHTML = voteable + " to vote";
}
</script>
</body>
</html>
Operatoren ??
returnerer det første argument, hvis det ikke er nullish (null
eller udefineret
).
Ellers returnerer det det andet argument.
let name = null;
let text = "missing";
let result = name ?? text;
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Operators</h1>
<h2>The ?? Operator</h2>
<p>The ?? operator returns the first argument if it is not nullish (null or undefined). Otherwise it returns the second.</p>
<p id="demo"></p>
<script>
let name = null;
let text = "missing";
let result = name ?? text;
document.getElementById("demo").innerHTML = "The name is " + result;
</script>
</body>
</html>
Nullish-operatøren er understøttet i alle browsere siden marts 2020:
Chrome 80 | Edge 80 | Firefox 72 | Safari 13.1 | Opera 67 |
Feb 2020 | Feb 2020 | Jan 2020 | Mar 2020 | Mar 2020 |
Operatoren ?.
returnerer undefined
, hvis et objekt er undefined
eller null
(i stedet for at smide en fejl).
// Create an object:
const car = {type:"Fiat", model:"500", color:"white"};
// Ask for car name:
document.getElementById("demo").innerHTML = car?.name;
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Operators</h1>
<h2>The ?. Operator</h2>
<p>The ?. operator returns undefined if an object is undefined or null (instead of throwing an error).</p>
<p>Car name is:</p>
<p id="demo"></p>
<script>
const car = {type:"Fiat", model:"500", color:"white"};
let name = car?.name;
document.getElementById("demo").innerHTML = name;
</script>
</body>
</html>
Den valgfri kædeoperatør er understøttet i alle browsere siden marts 2020:
Chrome 80 | Edge 80 | Firefox 72 | Safari 13.1 | Opera 67 |
Feb 2020 | Feb 2020 | Jan 2020 | Mar 2020 | Mar 2020 |