„JavaScript“ operatoriai (su pavyzdžiais)

Šioje pamokoje sužinosite apie įvairius „JavaScript“ operatorius ir kaip juos naudoti, naudodamiesi pavyzdžiais.

Kas yra operatorius?

„JavaScript“ operatorius yra specialus simbolis, naudojamas operandams (reikšmėms ir kintamiesiems) atlikti. Pavyzdžiui,

 2 + 3; // 5

Čia +yra operatorius, kuris atlieka papildymą 2ir 3yra operandas.

„JavaScript“ operatoriaus tipai

Čia pateikiamas įvairių operatorių, kuriuos sužinosite šioje pamokoje, sąrašas.

  • Užduočių operatoriai
  • Aritmetikos operatoriai
  • Palyginimo operatoriai
  • Loginiai operatoriai
  • Operatoriai bitais
  • Styginių operatoriai
  • Kiti operatoriai

„JavaScript“ priskyrimo operatoriai

Priskyrimo operatoriai naudojami priskirti reikšmes kintamiesiems. Pavyzdžiui,

 const x = 5;

Čia =operatorius naudojamas 5kintamajam priskirti vertę x.

Čia pateikiamas dažniausiai naudojamų priskyrimo operatorių sąrašas:

operatorius vardas Pavyzdys
= Užduoties operatorius a = 7; // 7
+= Papildymo priskyrimas a += 5; // a = a + 5
-= Atimties užduotis a -= 2; // a = a - 2
*= Daugybos užduotis a *= 3; // a = a * 3
/= Divizijos užduotis a /= 2; // a = a / 2
%= Likusi užduotis a %= 2; // a = a % 2
**= Išskyrimo užduotis a **= 2; // a = a**2

Pastaba: dažniausiai naudojamas priskyrimo operatorius yra =. Jūs suprasite kitus priskyrimo operatorius, kaip antai +=, -=, *=ir tt Kai mes mokomės aritmetines operacijas.

„JavaScript“ aritmetikos operatoriai

Aritmetiniai operatoriai naudojami atliekant aritmetinius skaičiavimus . Pavyzdžiui,

 const number = 3 + 5; // 8

Čia +operatorius naudojamas pridėti du operandus.

operatorius vardas Pavyzdys
+ Papildymas x + y
- Atimtis x - y
* Dauginimas x * y
/ Padalijimas x / y
% Priminimas x % y
++ Padidėjimas (padidėjimas 1) ++x arba x++
-- Mažinimas (mažinimas 1) --x arba x--
** Išsiplėtimas (galia) x ** y

1 pavyzdys: „JavaScript“ aritmetikos operatoriai

 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);

Norėdami sužinoti daugiau, apsilankykite ++ ir - operatoriuje.

Rezultatas

 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

Pastaba : ** operatorius buvo pristatytas „EcmaScript 2016“ ir kai kurios naršyklės gali jų nepalaikyti. Norėdami sužinoti daugiau, apsilankykite „Java“ palaikymo naršyklės palaikymo tarnyboje.

„JavaScript“ palyginimo operatoriai

Palyginimo operatoriai palygina dvi reikšmes ir pateikia loginę vertę truearba false. Pavyzdžiui,

 const a = 3, b = 2; console.log(a> b); // true 

Čia palyginimo operatorius >naudojamas norint palyginti, ar a yra didesnis nei b.

operatorius apibūdinimas Pavyzdys
== Equal to: returns true if the operands are equal x == y
!= Not equal to: returns true if the operands are not equal x != y
=== Strict equal to: true if the operands are equal and of the same type x === y
!== Strict not equal to: true if the operands are equal but of different type or not equal at all x !== y
> Greater than: true if left operand is greater than the right operand x> y
>= Greater than or equal to: true if left operand is greater than or equal to the right operand x>= y
< Less than: true if the left operand is less than the right operand x < y
<= Less than or equal to: true if the left operand is less than or equal to the right operand x <= y

Example 2: Comparison operators in 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 

Pastaba: Kai +jis naudojamas su stygomis, jis sujungiamas. Tačiau, kai +naudojamas su skaičiais, jis atlieka papildymą.

Kiti „JavaScript“ operatoriai

Čia pateikiamas kitų „JavaScript“ operatorių sąrašas. Apie šiuos operatorius sužinosite vėlesnėse pamokose.

operatorius apibūdinimas Pavyzdys
, įvertina kelis operandus ir grąžina paskutinio operando vertę. let a = (1, 3 , 4); // 4
?: grąžina vertę pagal sąlygą (5> 3) ? 'success' : 'error'; // "success"
delete ištrina objekto ypatybę arba masyvo elementą delete x
typeof pateikia eilutę, nurodančią duomenų tipą typeof 3; // "number"
void atmeta išraiškos grąžinimo vertę void(x)
in grąžinama, truejei nurodyta ypatybė yra objekte prop in object
instanceof grąžinama, truejei nurodytas objektas yra nurodyto objekto tipo object instanceof object_type

Įdomios straipsniai...