Šajā apmācībā jūs uzzināsit par dažādiem operatoriem, kas pieejami JavaScript, un par to, kā tos izmantot, izmantojot piemērus.
Kas ir operators?
JavaScript operētājsistēma ir īpašs simbols, ko izmanto operandu (vērtību un mainīgo) operāciju veikšanai. Piemēram,
2 + 3; // 5
Šeit +
ir operators, kas veic saskaitīšanu 2
un 3
ir operands.
JavaScript operatoru veidi
Šeit ir saraksts ar dažādiem operatoriem, kurus iemācīsities šajā apmācībā.
- Uzdevumu operatori
- Aritmētiskie operatori
- Operatoru salīdzinājums
- Loģiskie operatori
- Operatori bitiem
- Stīgu operatori
- Citi operatori
JavaScript piešķiršanas operatori
Piešķiršanas operatori tiek izmantoti, lai mainīgajiem piešķirtu vērtības. Piemēram,
const x = 5;
Lūk, tad =
operators tiek izmantots, lai piešķirtu vērtību 5
mainīgajam x
.
Šeit ir saraksts ar visbiežāk izmantotajiem piešķiršanas operatoriem:
Operators | Nosaukums | Piemērs |
---|---|---|
= | Uzdevuma operators | a = 7; // 7 |
+= | Papildinājumu piešķiršana | a += 5; // a = a + 5 |
-= | Atņemšanas uzdevums | a -= 2; // a = a - 2 |
*= | Reizināšanas uzdevums | a *= 3; // a = a * 3 |
/= | Divīzijas uzdevums | a /= 2; // a = a / 2 |
%= | Atlikušais uzdevums | a %= 2; // a = a % 2 |
**= | Paaugstināšanas uzdevums | a **= 2; // a = a**2 |
Piezīme. Parasti tiek izmantots piešķiršanas operators =
. Jūs saprotat citas uzdevuma operatorus, piemēram +=
, -=
, *=
uc, kad mēs uzzinām aritmētiskās darbības.
JavaScript aritmētiskie operatori
Aritmētiskos operatorus izmanto, lai veiktu aritmētiskos aprēķinus . Piemēram,
const number = 3 + 5; // 8
Šeit +
operators tiek izmantots, lai pievienotu divus operandus.
Operators | Nosaukums | Piemērs |
---|---|---|
+ | Papildinājums | x + y |
- | Atņemšana | x - y |
* | Reizināšana | x * y |
/ | Nodaļa | x / y |
% | Atlikušais | x % y |
++ | Pieaugums (pieaugums par 1) | ++x vai x++ |
-- | Samazinājums (samazinājums par 1) | --x vai x-- |
** | Eksponentācija (jauda) | x ** y |
1. piemērs: Aritmētiskie operatori JavaScript
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);
Lai uzzinātu vairāk, apmeklējiet operatoru ++ un -.
Rezultāts
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
Piezīme : Operators ** tika ieviests EcmaScript 2016, un dažas pārlūkprogrammas tos, iespējams, neatbalsta. Lai uzzinātu vairāk, apmeklējiet JavaScript paplašināšanas pārlūka atbalstu.
JavaScript salīdzināšanas operatori
Salīdzinājums operatori salīdzināt divas vērtības un atgriešanās Būla vērtību, vai nu true
vai false
. Piemēram,
const a = 3, b = 2; console.log(a> b); // true
Šeit salīdzināšanas operatoru >
izmanto, lai salīdzinātu, vai a ir lielāks par b.
Operators | Apraksts | Piemērs |
---|---|---|
== | Vienāds ar : atgriež, true ja operandi ir vienādi | x == y |
!= | Nav vienāds ar : atgriežas, true ja operandi nav vienādi | x != y |
=== | Stingri vienāds ar : true ja operandi ir vienādi un tā paša veida | x === y |
!== | Stingri nav vienādi ar : true ja operandi ir vienādi, bet dažāda veida vai nemaz nav vienādi | x !== y |
> | Lielāks nekā : true ja kreisais operands ir lielāks par labo operandu | x> y |
>= | Lielāks par vai vienāds ar : true ja kreisais operands ir lielāks vai vienāds ar labo operandu | x>= y |
< | Mazāk nekā : true ja kreisais operands ir mazāks par labo operandu | x < y |
<= | Mazāks vai vienāds ar : true ja kreisais operands ir mazāks vai vienāds ar labo operandu | x <= y |
2. piemērs: salīdzināšanas operatori JavaScript
// 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
Piezīme: Ja +
to lieto kopā ar virknēm, tas veic savienošanu. Tomēr, ja +
to lieto kopā ar skaitļiem, tas veic papildinājumu.
Citi JavaScript operatori
Šeit ir saraksts ar citiem operatoriem, kas pieejami JavaScript. Jūs uzzināsiet par šiem operatoriem turpmākajās apmācībās.
Operators | Apraksts | Piemērs |
---|---|---|
, | novērtē vairākus operandus un atgriež pēdējā operanda vērtību. | let a = (1, 3 , 4); // 4 |
?: | atgriež vērtību, pamatojoties uz nosacījumu | (5> 3) ? 'success' : 'error'; // "success" |
delete | izdzēš objekta īpašumu vai masīva elementu | delete x |
typeof | atgriež virkni, kas norāda datu tipu | typeof 3; // "number" |
void | izmet izteiksmes atgriešanās vērtību | void(x) |
in | atgriež, true ja norādītais rekvizīts atrodas objektā | prop in object |
instanceof | atgriežas, true ja norādītajam objektam ir norādītais objekta tips | object instanceof object_type |