Tässä opetusohjelmassa opit erilaisista JavaScript-operaattoreista ja niiden käytöstä esimerkkien avulla.
Mikä on operaattori?
JavaScriptissä operaattori on erityinen symboli, jota käytetään operandien (arvojen ja muuttujien) suorittamiseen. Esimerkiksi,
2 + 3; // 5
Tässä +
on operaattori, joka suorittaa lisäyksen 2
ja 3
ovat operandeja.
JavaScript-operaattorityypit
Tässä on luettelo eri operaattoreista, jotka opit tässä opetusohjelmassa.
- Tehtäväoperaattorit
- Aritmeettiset operaattorit
- Vertailuoperaattorit
- Loogiset operaattorit
- Bit-operaattorit
- Jousisoitinoperaattorit
- Muut operaattorit
JavaScript-määritysoperaattorit
Määritysoperaattoreita käytetään määrittämään arvot muuttujille. Esimerkiksi,
const x = 5;
Tässä =
operaattoria käytetään määrittämään arvo 5
muuttujalle x
.
Tässä on luettelo yleisesti käytetyistä käyttöoperaattoreista:
Operaattori | Nimi | Esimerkki |
---|---|---|
= | Tehtäväoperaattori | a = 7; // 7 |
+= | Lisäysmääritys | a += 5; // a = a + 5 |
-= | Vähennyslasku | a -= 2; // a = a - 2 |
*= | Kertolasku | a *= 3; // a = a * 3 |
/= | Jaoston tehtävä | a /= 2; // a = a / 2 |
%= | Loput tehtävät | a %= 2; // a = a % 2 |
**= | Exponentiation-tehtävä | a **= 2; // a = a**2 |
Huomaa: Yleisesti käytetty määritysoperaattori on =
. Ymmärrätte muita eraattoreita kuten +=
, -=
, *=
jne, kun opimme laskutoimitukset.
JavaScript-aritmeettiset operaattorit
Aritmeettisia operaattoreita käytetään aritmeettisten laskelmien suorittamiseen . Esimerkiksi,
const number = 3 + 5; // 8
Tässä +
operaattoria käytetään lisäämään kaksi operandia.
Operaattori | Nimi | Esimerkki |
---|---|---|
+ | Lisäys | x + y |
- | Vähennyslasku | x - y |
* | Kertolasku | x * y |
/ | Divisioona | x / y |
% | Loput | x % y |
++ | Lisäys (1: n lisäys) | ++x tai x++ |
-- | Vähennys (vähennykset 1) | --x tai x-- |
** | Eksponentio (teho) | x ** y |
Esimerkki 1: Aritmeettiset operaattorit JavaScriptissä
let x = 5; let y = 3; // addition console.log('x + y = ', x + y); // subtraction console.log('x - y = ', x - y); // multiplication console.log('x * y = ', x * y); // division console.log('x / y = ', x / y); // remainder console.log('x % y = ', x % y); // increment console.log('++x = ', ++x); // x is now 6 console.log('x++ = ', x++); // x returns 6 and then increases by 1 console.log('x = ', x); // decrement console.log('--x = ', --x); // x is now 6 console.log('x-- = ', x--); // x returns 6 and then increases by 1 console.log('x = ', x); //exponentiation console.log('x ** y =', x ** y);
Käy ++ - ja - operaattorissa saadaksesi lisätietoja.
Tuotos
x + y = 8 x - y = 2 x * y = 15 x / y = 1.6666666666666667 x% y = 2 ++ x = 6 x ++ = 6 x = 7 --x = 6 x-- = 6 x = 5 x ** y = 125
Huomaa : ** -operaattori otettiin käyttöön EcmaScript 2016: ssa, ja jotkin selaimet eivät ehkä tue niitä. Saat lisätietoja käymällä JavaScript-eksponentointiselaimen tuessa.
JavaScript-vertailuoperaattorit
Vertailuoperaattorit vertaavat kahta arvoa ja palauttavat loogisen arvon, joko true
tai false
. Esimerkiksi,
const a = 3, b = 2; console.log(a> b); // true
Tässä vertailuoperaattoria >
käytetään vertaamaan, onko a suurempi kuin b.
Operaattori | Kuvaus | Esimerkki |
---|---|---|
== | Yhtä kuin : palauttaa, true jos operandit ovat samat | x == y |
!= | Ei yhtä suuri : palauttaa, true jos operandit eivät ole yhtä suuret | x != y |
=== | Tarkka yhtä : true jos operandit ovat samanarvoisia ja samantyyppisiä | x === y |
!== | Tarkka ei ole yhtä suuri kuin : true jos operandit ovat samanarvoisia, mutta erityyppisiä tai eivät lainkaan | x !== y |
> | Suurempi kuin : true jos vasen operandi on suurempi kuin oikea operandi | x> y |
>= | Suurempi tai yhtä suuri kuin : true jos vasen operandi on suurempi tai yhtä suuri kuin oikea operandi | x>= y |
< | Pienempi kuin : true jos vasen operandi on pienempi kuin oikea operandi | x < y |
<= | Pienempi tai yhtä suuri kuin : true jos vasen operandi on pienempi tai yhtä suuri kuin oikea operandi | x <= y |
Esimerkki 2: Vertailuoperaattorit JavaScriptissä
// equal operator console.log(2 == 2); // true console.log(2 == '2'); // true // not equal operator console.log(3 != 2); // true console.log('hello' != 'Hello'); // true // strict equal operator console.log(2 === 2); // true console.log(2 === '2'); // false // strict not equal operator console.log(2 !== '2'); // true console.log(2 !== '2'); // false
Output
true true true true true false false true
Comparison operators are used in decision making and loops. You will learn about the use of comparison operators in detail in later tutorials.
JavaScript Logical Operators
Logical operators perform logical operations and return a boolean value, either true
or false
. For example,
const x = 5, y = 3; (x < 6) && (y < 5); // true
Here, &&
is the logical operator AND. Since both x < 6
and y < 5
are true
, the result is true
.
Operator | Description | Example |
---|---|---|
&& | Logical AND: true if both the operands are true , else returns false | x && y |
|| | Logical OR: true if either of the operands is true ; returns false if both are false | x || y |
! | Logical NOT: true if the operand is false and vice-versa. | !x |
Example 3: Logical Operators in JavaScript
// logical AND console.log(true && true); // true console.log(true && false); // false // logical OR console.log(true || false); // true // logical NOT console.log(!true); // false
Output
true false true false
Logical operators are used in decision making and loops. You will learn about the use of logical operators in detail in later tutorials.
JavaScript Bitwise Operators
Bitwise operators perform operations on binary representations of numbers.
Operator | Description |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise NOT |
<< | Left shift |
>> | Sign-propagating right shift |
>>> | Zero-fill right shift |
Bitwise operators are rarely used in everyday programming. If you are interested, visit JavaScript Bitwise Operators to learn more.
JavaScript String Operators
In JavaScript, you can also use the +
operator to concatenate (join) two or more strings.
Example 4: String operators in JavaScript
// concatenation operator console.log('hello' + 'world'); let a = 'JavaScript'; a += ' tutorial'; // a = a + ' tutorial'; console.log(a);
Output
helloworld JavaScript tutorial
Huomaa: Kun +
sitä käytetään merkkijonojen kanssa, se ketjuttaa. Kuitenkin, kun +
sitä käytetään numeroiden kanssa, se suorittaa lisäyksen.
Muut JavaScript-operaattorit
Tässä on luettelo muista operaattoreista, jotka ovat käytettävissä JavaScript-muodossa. Opit näistä operaattoreista myöhemmissä opetusohjelmissa.
Operaattori | Kuvaus | Esimerkki |
---|---|---|
, | arvioi useita operandeja ja palauttaa viimeisen operandin arvon. | let a = (1, 3 , 4); // 4 |
?: | palauttaa arvon ehdon perusteella | (5> 3) ? 'success' : 'error'; // "success" |
delete | poistaa objektin ominaisuuden tai matriisin elementin | delete x |
typeof | palauttaa tietotyypin osoittavan merkkijonon | typeof 3; // "number" |
void | hylkää lausekkeen palautusarvon | void(x) |
in | palauttaa, true jos määritetty ominaisuus on objektissa | prop in object |
instanceof | palauttaa, true jos määritetty objekti on määritettyä objektityyppiä | object instanceof object_type |