Aritmeettiset operaattorit suorittavat aritmeettisia lukuja (literaaleja tai muuttujia).
Lisäys
Vähennyslasku
Kertominen
Eksponentti (ES2016)
Division
Modulus (jäljellä)
Lisäys
Vähennys
Tyypillinen aritmeettinen operaatio toimii kahdella numerolla.
Nämä kaksi numeroa voivat olla literaaleja:
let x = 100 + 50;Kokeile itse →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arithmetic</h2>
<p>A typical arithmetic operation takes two numbers and produces a new number.</p>
<p id="demo"></p>
<script>
let x = 100 + 50;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
tai muuttujat:
let x = a + b;Kokeile itse →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arithmetic</h2>
<p>A typical arithmetic operation takes two numbers (or variables) and produces a new number.</p>
<p id="demo"></p>
<script>
let a = 100;
let b = 50;
let x = a + b;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
tai ilmaisuja:
let x = (100 + 50) * a;Kokeile itse →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>Arithmetic Operations</h2>
<p>A typical arithmetic operation takes two numbers (or expressions) and produces a new number.</p>
<p id="demo"></p>
<script>
let a = 3;
let x = (100 + 50) * a;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
Numeroita (aritmeettisessa operaatiossa) kutsutaan operandeiksi.
Toiminnon (joka suoritetaan kahden operandin välillä) määrittää operaattori.
| Operand | Operator | Operand |
|---|---|---|
| 100 | + | 50 |
Addition-operaattori (+) lisää numeroita:
let x = 5;
let y = 2;
let z = x + y;Kokeile itse →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>The + Operator</h2>
<p id="demo"></p>
<script>
let x = 5;
let y = 2;
let z = x + y;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
Vähennysoperaattori (-) vähentää lukuja.
let x = 5;
let y = 2;
let z = x - y;Kokeile itse →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>The - Operator</h2>
<p id="demo"></p>
<script>
let x = 5;
let y = 2;
let z = x - y;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
Kerto-operaattori (*) kertoo numerot.
let x = 5;
let y = 2;
let z = x * y;Kokeile itse →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>The * Operator</h2>
<p id="demo"></p>
<script>
let x = 5;
let y = 2;
let z = x * y;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
jako-operaattori (/) jakaa numerot.
let x = 5;
let y = 2;
let z = x / y;Kokeile itse →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>The / Operator</h2>
<p id="demo"></p>
<script>
let x = 5;
let y = 2;
let z = x / y;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
Moduuli-operaattori (%) palauttaa jakojäännöksen.
let x = 5;
let y = 2;
let z = x % y;Kokeile itse →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>The % Operator</h2>
<p id="demo"></p>
<script>
let x = 5;
let y = 2;
let z = x % y;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
Aritmetiikassa kahden kokonaisluvun jakaminen tuottaa osamäärän ja loppuosa.
Matematiikassa modulo-operaation tulos on aritmeettisen jaon jäännös.
Increment-operaattori (++) lisää numeroita.
let x = 5;
x++;
let z = x;Kokeile itse →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>The ++ Operator</h2>
<p id="demo"></p>
<script>
let x = 5;
x++;
let z = x;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
Decrement-operaattori (--) pienentää lukuja.
let x = 5;
x--;
let z = x;Kokeile itse →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>The -- Operator</h2>
<p id="demo"></p>
<script>
let x = 5;
x--;
let z = x;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
Exponentointi-operaattori (**) nostaa ensimmäisen operandin toisen operandin potenssiin.
let x = 5;
let z =
x ** 2;Kokeile itse →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>The ** Operator</h2>
<p id="demo"></p>
<script>
let x = 5;
document.getElementById("demo").innerHTML = x ** 2;
</script>
</body>
</html>
x ** y tuottaa saman tuloksen kuin Math.pow(x,y):
let x = 5;
let z =
Math.pow(x,2);Kokeile itse →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>Math.pow()</h2>
<p id="demo"></p>
<script>
let x = 5;
document.getElementById("demo").innerHTML = Math.pow(x,2);
</script>
</body>
</html>
Operaattorin ensisijaisuus kuvaa järjestystä, jossa toiminnot suoritetaan aritmeettinen lauseke.
let x = 100 + 50 * 3;Kokeile itse →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>Operator Precedence</h2>
<p>Multiplication has precedence over addition.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 100 + 50 * 3;
</script>
</body>
</html>
Onko yllä olevan esimerkin tulos sama kuin 150 * 3 vai onko se sama kuin 100 + 150?
Tehdäänkö yhteen- vai kertolasku ensin?
Kuten perinteisessä koulumatematiikassa, kertolasku tehdään ensin.
Kertolaskulla (*) ja jaolla (/) on suurempi etusija kuin yhteenlasku (+) ja vähennyslasku (-).
Ja (kuten koulumatematiikassa) etusijaa voidaan muuttaa käyttämällä suluissa.
Sulkuja käytettäessä lasketaan sulkujen sisällä olevat toiminnot ensimmäinen:
let x = (100 + 50) * 3;Kokeile itse →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>Operator Precedence</h2>
<p>Multiplication has precedence over addition.</p>
<p>But parenthesis has precedence over multiplication.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = (100 + 50) * 3;
</script>
</body>
</html>
Kun monilla operaatioilla on sama etusija (kuten yhteenlasku ja vähennys- tai kerto- ja jakolasku), ne lasketaan vasemmalta oikein:
let x = 100 + 50 - 3;Kokeile itse →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>Operator Precedence</h2>
<p>When many operations has the same precedence, they are computed from left to right.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 100 + 50 - 3;
</script>
</body>
</html>
let x = 100 / 50 * 3;Kokeile itse →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>Operator Precedence</h2>
<p>When many operations has the same precedence, they are computed from left to right.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 100 / 50 * 3;
</script>
</body>
</html>
Täydellinen luettelo operaattorin prioriteettiarvoista on osoitteessa:
JavaScript-operaattorin ensisijaisuusarvot.