Arithmetic Operator
Arithmatic Operator หรือ Operator ที่เกี่ยวกับตัวเลข เป็น Operator ที่ ธรรมดาที่สุด เพราะเราใช้อยู่ทุกวี่ทุกวันในการคำนวนต่างๆ JavaScirpt ก็มี Operator ให้เราใช้ดังนี้ครับ+ | บวก |
- | ลบ |
* | คูณ |
/ | หาร |
% | การหารเอาเศษเป็นผลลัพธ์ (MOD) |
-- | การลดค่า 1 ให้ตัวแปร |
++ | การเพิ่มค่า 1 ให้ตัวแปร |
Logical Operator
Logical Operator จะใช้ในการคำนวนทางตรรก เช่น And Or NotLogical Operator ใน JavaScript
Comparison Operator
เป็น Operator ที่ใช้เปรียบเที่ยบข้อมูล 2 ตัวนะครับ มีอะไรมั่งเรามาดูกันAssignment Operator
Assignment Operator ก็คือ Operator ที่ใช้ในการใส่ให้ค่ากับตัวแปร Operator ที่เป็นพื้นฐานก็คือ = ซึ่งใช้ในการให้ค่ากับตัวแปรโดยตรง นอกจากนี้ยังงมี Operator ตัวอื่น ที่สามารถ คำนวนค่าก่อนแล้วค่อยเอาไปเก็บในตัวแปร เช่น A += 30 ก็จะหมายถึง เอา A บวกด้วย 30 แล้วเก็บ ไว้ใน A เหมือนเดิม ใน Section นี้เราจะมาดูกันนะครับว่า Assignment Operator ใน JavaScript จะมีอะไรบ้าง= | ใส่ค่าของทางขวาให้ตัวแปรทางซ้าย |
+= | เอาทางซ้ายบวกทางขวาแล้วเก็บไว้ทางซ้าย |
-= | เอาทางซ้ายลบทางขวาแล้วเก็บไว้ทางซ้าย |
*= | เอาทางซ้ายคูณทางขวาแล้วเก็บไว้ทางซ้าย |
/= | เอาทางซ้ายหารทางขวาแล้วเก็บไว้ทางซ้าย |
%= | เอาทางซ้ายหารทางขวาแล้วเก็บเศษที่ได้ไว้ทางซ้าย |
ตารางตัวอย่างการใช้ Operator
Category | Operator | Description | Usage Example | Value/Result |
String | + | concatenation | "Java" + "Script" | JavaScript |
Arithmetic | + | addition | 2 + 3 | 5 |
- | subtraction | 6 - 4 | 2 | |
unary negation | -9 | -9 | ||
* | multiplication | 3 * 4 | 12 | |
/ | division | 15/3 | 5 | |
% | modulus | 15%7 | 1 | |
++ | increment and then return value | x=3; ++x | 4 | |
return value and then increment | x=3; x++ | 3 | ||
-- | decrement and then return value | x=3; --x | 2 | |
return value and then decrement | x=3; x-- | 3 | ||
Bit Manipulation | & | and | 10 & 7 | 2 |
| | or | 10 | 7 | 15 | |
^ | exclusive or | 10 ^ 7 | 13 | |
<< | left shift | 7 << 3 | 56 | |
>> | sign-propagating right shift | -7 >> 2 | -2 | |
>>> | zero-fill right shift | -7 >>> 2 | 1073741822 | |
Logical | && | logical and | true && false | false |
|| | logical or | true || false | true | |
! | not | !true | false | |
Comparison | == | equal | 3 == 7 | false |
!= | not equal | 3 != 7 | true | |
< | less than | 3 < 7 | true | |
<= | less than or equal | 3 <= 7 | true | |
> | greater than | 3 > 7 | false | |
>= | greater than or equal | 3 >= 7 | false | |
Conditional Expression | (condition) ? value1 : value2 | if condition is true then value1 else value2 | true ? 3 : 7 | 3 |
ไม่มีความคิดเห็น:
แสดงความคิดเห็น