Mga Operator sa JavaScript
- Previous Page JS Const
- Next Page JS Arithmetic
Mga Operator sa JavaScript
Example
Mga hakbang sa paghahati ng variable at pagpapatapos nito:
var x = 7; // Maghahati sa x ang 7 var y = 8; // Maghahati sa y ang 8 var z = x + y; // Assign value to z (x + y) 15
AssignmentOperator (=
) Assign value to variable.
Assignment
var x = 15;
AdditionOperator (+
) Add numbers:
Addition
var x = 7; var y = 8; var z = x + y;
MultiplicationOperator (*
) Multiply numbers:
Multiplication
var x = 7; var y = 8; var z = x * y;
JavaScript Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on numbers:
Operator | Description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (remainder) |
++ | Increment |
-- | Decrement |
Komentaryo:JS ArithmeticThis chapter fully describes arithmetic operators.
JavaScript Assignment Operator
The assignment operator assigns a value to a JavaScript variable.
Operator | example | 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 |
Addition Assignment Operator (+=
) Add a value to the variable.
Assignment
var x = 7; x += 8;
Komentaryo:JS AssignmentThis chapter fully describes the assignment operator.
JavaScript String Operators
+
The operator can also be used to add (concatenate, cascade) strings.
Example
txt1 = "Bill"; txt2 = "Gates"; txt3 = txt1 + " " + txt2;
The result of txt3 will be:
Bill Gates
+=
The assignment operator can also be used to add (cascading) strings:
Example
txt1 = "Hello "; txt1 += "Kitty!";
The result of txt1 will be:
Hello Kitty!
Tip:When used for strings:+
The operator is called the cascading operator.
Addition of string and number
Adding two numbers will return the sum, but adding a number and a string will return a string:
Example
x = 7 + 8; y = "7" + 8; z = "Hello" + 7;
The result of x, y and z will be:
15 78 Hello7
Tip:If you add a number and a string, the result will be a string!
JavaScript Comparison Operators
Operator | Description |
---|---|
== | Equal |
=== | Equal in value and type |
!= | Not equal |
!== | Not equal or not of the same type |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
? | Ternary Operator |
Komentaryo:JS ComparisonIto ay naglalarawan nang buong-buong ang mga comparison operator.
Logic operator ng JavaScript
Operator | Description |
---|---|
&& | Logic AND |
|| | Logic OR |
! | Logic NOT |
Komentaryo:JS ComparisonIto ay naglalarawan nang buong-buong ang mga logic operator.
Type operator ng JavaScript
Operator | Description |
---|---|
typeof | Ibinabalik ang uri ng variable. |
instanceof | Ibinabalik ang true kung ang bagay ay isang instance ng uri ng bagay. |
Komentaryo:JS Type ConversionIto ay naglalarawan nang buong-buong ang mga type operator.
Bitwise Operators sa JavaScript
Ang mga operator ng bit ay gumagawa ng operasyon sa 32-bit na numero.
Anumang numero na magiging bahagi ng opeytor ay maging 32-bit na numero. Ang resulta ay ibabalik sa numero ng JavaScript.
Operator | Description | example | equivalent to | Result | Decimal |
---|---|---|---|---|---|
& | AND | 5 & 1 | 0101 & 0001 | 0001 | 1 |
| | OR | 5 | 1 | 0101 | 0001 | 0101 | 5 |
~ | Negation | ~ 5 | ~0101 | 1010 | 10 |
^ | XOR | 5 ^ 1 | 0101 ^ 0001 | 0100 | 4 |
<< | Left shift with zero-padding | 5 << 1 | 0101 << 1 | 1010 | 10 |
>> | Signed right shift | 5 >> 1 | 0101 >> 1 | 0010 | 2 |
>>> | Right shift with zero-padding | 5 >>> 1 | 0101 >>> 1 | 0010 | 2 |
Ang halimbawa sa paggamit ng apat na bit na walang signo. Subalit ang JavaScript ay gumagamit ng 32-bit na may signo.
Kaya't sa JavaScript, ~5 ay hindi magbibigay ng 10, kundi -6.
~00000000000000000000000000000101 ay magbibigay ng 11111111111111111111111111111010.
Komentaryo:Magiging kasama kami sa JS Bit OperationIto ay naglalarawan nang buong-buong kung paano gumagana ang mga operator ng bit.
bukas na aklat
kailangan mong maghanap ng mas maraming kaalaman tungkol sa Mga Operator sa JavaScriptPara sa karagdagang kaalaman tungkol sa
- ECMAScript Unary Operators
- Ang isang-wangis na operator ay may isang parameter lamang, na ang bagay o halaga na dapat ioperahan. Ito ay naglalarawan ng pinakasimpleng operator ng ECMAScript - ang isang-wangis na operator.
- ECMAScript Bitwise Operators
- Ang mga operator ng bit ay gumagawa ng operasyon sa harap ng numero. Ito ay naglalarawan nang malalim ang kaalaman tungkol sa integer, at nagpapakilala sa iba't ibang operator ng bit ng ECMAScript.
- ECMAScript Boolean Operators
- Ang mga operator ng Boolean ay napakahalaga. Ito ay naglalarawan nang malalim ang tatlong operator ng Boolean: NOT, AND at OR.
- ECMAScript Multiplicative Operators
- Ito ay naglalarawan kung paano gumagana ang mga pagsasamang pagkatanim ng ECMAScript: ang pagkatanim, paghahati at pagsasama ng mod, at ang kanilang espesyal na pag-uugali.
- ECMAScript Additive Operators
- Ito ay naglalarawan kung paano gumagana ang mga pagsasama ng ECMAScript: ang pagsasama at pagsasalba, at ang kanilang espesyal na pag-uugali.
- ECMAScript Relational Operators
- Relational operators perform comparison operations. This section explains the conventional comparison methods of relational operators, as well as how to compare strings with numbers.
- ECMAScript Equality Operators
- Equality operators are used to determine if variables are equal. ECMAScript provides two sets of equality operators: equal to and not equal to, as well as strictly equal to and strictly not equal to.
- ECMAScript Conditional Operators
- This section explains the conditional operator in ECMAScript.
- ECMAScript Assignment Operators
- This section explains the assignment operator in ECMAScript.
- ECMAScript Comma Operator
- This section explains the comma operator in ECMAScript.
- Previous Page JS Const
- Next Page JS Arithmetic