Operatorzy JavaScript

Operatorzy JavaScript

Przykład

Przypisanie wartości do zmiennych i dodawanie ich:

var x = 7;		// Przypisanie wartości 7 do zmiennej x
var y = 8;		// Przypisanie wartości 8 do zmiennej y
var z = x + y;		// Przypisanie wartości do z (x + y) 15

Spróbuj sam

PrzypisanieOperator (=)przypisanie wartości do zmiennej.

Przypisanie

var x = 15;

Spróbuj sam

DodawanieOperator (+)dodawanie liczb:

Dodawanie

var x = 7;
var y = 8;
var z = x + y;

Spróbuj sam

MnożenieOperator (*)pomnożenie liczb:

Mnożenie

var x = 7;
var y = 8;
var z = x * y; 

Spróbuj sam

Operatory arytmetyczne JavaScript

Operatory arytmetyczne są używane do wykonywania operacji arytmetycznych na liczbach:

Operator Description
+ Dodawanie
- Odejmowanie
* Mnożenie
/ Dzielenie
% Pobieranie reszty (reszty)
++ Zwiększenie
-- Zmniejszenie

Note:JS algebraW tym rozdziale szczegółowo opisano operatory arytmetyczne.

Operator przypisania JavaScript

Operator przypisania przypisuje wartość do zmiennej JavaScript.

Operator Example is equivalent to
= x = y x = y
+= x += y x = x + y
-= x -= y x = x - y
*= x *= y x = x * y
/= x /= y x = x / y
%= x %= y x = x % y

Dodać operator przypisania (+=)dodaje wartość do zmiennej.

Przypisanie

var x = 7;
x += 8; 

Spróbuj sam

Note:JS przypisanieW tym rozdziale szczegółowo opisano operator przypisania.

Operatory łańcuchowe JavaScript

+ Operator można również używać do dodawania (łączenia, kaskadowego) ciągów znaków.

Przykład

txt1 = "Bill";
txt2 = "Gates";
txt3 = txt1 + " " + txt2; 

Wynik txt3 będzie:

Bill Gates

Spróbuj sam

+= Operator przypisania można również używać do dodawania (kaskadowego) ciągów znaków:

Przykład

txt1 = "Hello ";
txt1 += "Kitty!"; 

Wynik txt1 będzie:

Hello Kitty!

Spróbuj sam

Wskazówka:Kiedy używane są do ciągów znaków:+ Operatory te nazywane są operatorami kaskadowymi.

Dodawanie ciągu znaków i liczby

Dodanie dwóch liczb zwróci sumę, ale dodanie liczby i ciągu znaków zwróci ciąg znaków:

Przykład

x = 7 + 8;
y = "7" + 8;
z = "Hello" + 7;

Wynik x, y i z będzie:

15
78
Hello7

Spróbuj sam

Wskazówka:Jeśli dodasz liczbę i ciąg znaków, wynik będzie ciągiem znaków!

Porównawcze operatory JavaScript

Operator Description
== Równy
=== Równy typ
!= Nie równy
!== Nie równy lub różny typ
> Większy
< Mniej
>= Większy lub równy
<= Mniej lub równy
? Trójargumentowy operator

Note:JS ComparisonThis chapter fully describes comparison operators.

JavaScript logical operators

Operator Description
&& Logical AND
|| Logical OR
! Logical NOT

Note:JS ComparisonThis chapter fully describes logical operators.

JavaScript type operators

Operator Description
typeof Returns the type of the variable.
instanceof Returns true if the object is an instance of the object type.

Note:JS Type ConversionThis chapter fully describes the type operator.

Operatorzy bitowe w JavaScript

Bitwise operators handle 32-bit numbers.

Any numerical operand in this operation will be converted to a 32-bit number. The result will be converted back to a JavaScript number.

Operator Description Example is equivalent to Result Decimal
& AND 5 & 1 0101 & 0001 0001 1
| OR 5 | 1 0101 | 0001 0101 5
~ NOT ~ 5 ~0101 1010 10
^ XOR 5 ^ 1 0101 ^ 0001 0100 4
<< Zero-filled left shift 5 << 1 0101 << 1 1010 10
>> Signed right shift 5 >> 1 0101 >> 1 0010 2
>>> Zero-filled right shift 5 >>> 1 0101 >>> 1 0010 2

The previous example used a 4-bit unsigned example. However, JavaScript uses 32-bit signed numbers.

Therefore, in JavaScript, ~5 will not return 10, but -6.

~00000000000000000000000000000101 will return 11111111111111111111111111111010.

Note:We will JS BitwiseThis chapter explains bitwise operators in detail.

extra books

For more information on Operatorzy JavaScriptFor more information on the knowledge, please read the relevant content in the advanced JavaScript tutorial:

Jednonarzędniowe operatory ECMAScript
Unary operators have only one parameter, that is, the object or value to be operated on. This section explains the simplest operator in ECMAScript - unary operator.
Bitowe operatory ECMAScript
Bitwise operators operate at the bottom level of numbers. This section delves deeply into the knowledge of integers and introduces various bitwise operators of ECMAScript.
Logiczne operatory ECMAScript
Boolean operators are very important. This section delves deeply into three Boolean operators: NOT, AND, and OR.
Mnożnicze operatory ECMAScript
This section explains the multiplicative operators of ECMAScript: multiplication, division, modulus operators, and their special behaviors.
Dodawcze operatory ECMAScript
To section explains the additive operators of ECMAScript: addition, subtraction operators, and their special behaviors.
Relacyjne operatory ECMAScript
Relacyjne operatory wykonują porównania. Ten dział wyjaśnia typowe metody porównywania operatorów relacyjnych oraz jak porównywać ciąg znaków z liczbami.
Operatory równości ECMAScript
Operatory równości są używane do sprawdzania, czy zmienne są równe. ECMAScript oferuje dwa zestawy operatorów równości: znak równości i znak nierówności, oraz pełny znak równości i pełny znak nierówności.
Warunkowe operatory ECMAScript
Ten dział wyjaśnia warunkowy operator w ECMAScript.
Operator przypisania ECMAScript
Ten dział wyjaśnia operator przypisania w ECMAScript.
Operator przecinka w ECMAScript
Ten dział wyjaśnia operator przecinka w ECMAScript.

Zobacz również

Kurs:Priorytety operatorów w JavaScript

Podręcznik referencyjny:Operatorzy JavaScript