วันจันทร์ที่ 22 เมษายน พ.ศ. 2556

Operater


Arithmetic Operator

          Arithmatic Operator หรือ Operator ที่เกี่ยวกับตัวเลข เป็น Operator ที่ ธรรมดาที่สุด เพราะเราใช้อยู่ทุกวี่ทุกวันในการคำนวนต่างๆ JavaScirpt ก็มี Operator ให้เราใช้ดังนี้ครับ
+บวก
-ลบ
*คูณ
/หาร
%การหารเอาเศษเป็นผลลัพธ์ (MOD)
--การลดค่า 1 ให้ตัวแปร
++การเพิ่มค่า 1 ให้ตัวแปร
<html> <head><title>Usage Arithmatic Operator</title></head> <body> <script language="JavaScript"> <!-- document.write('105 + 20 = '); document.write(105 + 20+'<br>'); document.write('105 - 20 = '); document.write(105 - 20+'<br>'); document.write('105 * 20 = '); document.write(105 * 20+'<br>'); document.write('105 / 20 = '); document.write(105 / 20+'<br>'); document.write('105 % 20 = '); document.write(105 % 20+'<br>'); // --> </script> </body> </html>

Logical Operator

Logical Operator จะใช้ในการคำนวนทางตรรก เช่น And Or Not 
Logical Operator ใน JavaScript
&&And
||Or
!Not

Comparison Operator

เป็น Operator ที่ใช้เปรียบเที่ยบข้อมูล 2 ตัวนะครับ มีอะไรมั่งเรามาดูกัน
==เท่ากับ
!=ไม่เท่ากับ
<น้อยกว่า
>มากกว่า
<=น้อยกว่าเท่ากับ
>=มากกว่าเท่ากับ

Assignment Operator

          Assignment Operator ก็คือ Operator ที่ใช้ในการใส่ให้ค่ากับตัวแปร Operator ที่เป็นพื้นฐานก็คือ = ซึ่งใช้ในการให้ค่ากับตัวแปรโดยตรง นอกจากนี้ยังงมี Operator ตัวอื่น ที่สามารถ คำนวนค่าก่อนแล้วค่อยเอาไปเก็บในตัวแปร เช่น A += 30 ก็จะหมายถึง เอา A บวกด้วย 30 แล้วเก็บ ไว้ใน A เหมือนเดิม ใน Section นี้เราจะมาดูกันนะครับว่า Assignment Operator ใน JavaScript จะมีอะไรบ้าง
=ใส่ค่าของทางขวาให้ตัวแปรทางซ้าย
+=เอาทางซ้ายบวกทางขวาแล้วเก็บไว้ทางซ้าย
-=เอาทางซ้ายลบทางขวาแล้วเก็บไว้ทางซ้าย
*=เอาทางซ้ายคูณทางขวาแล้วเก็บไว้ทางซ้าย
/=เอาทางซ้ายหารทางขวาแล้วเก็บไว้ทางซ้าย
%=เอาทางซ้ายหารทางขวาแล้วเก็บเศษที่ได้ไว้ทางซ้าย

ตารางตัวอย่างการใช้ Operator


CategoryOperatorDescriptionUsage ExampleValue/Result
String+concatenation"Java" + "Script"JavaScript
Arithmetic+addition2 + 35
-subtraction6 - 42
unary negation-9-9
*multiplication3 * 412
/division15/35
%modulus15%71
++increment and then return valuex=3; ++x4
return value and then incrementx=3; x++3
--decrement and then return valuex=3; --x2
return value and then decrementx=3; x--3
Bit Manipulation&and10 & 72
|or10 | 715
^exclusive or10 ^ 713
<<left shift7 << 356
>>sign-propagating right shift-7 >> 2-2
>>>zero-fill right shift-7 >>> 21073741822
Logical&&logical andtrue && falsefalse
||logical ortrue || falsetrue
!not!truefalse
Comparison==equal3 == 7false
!=not equal3 != 7true
<less than3 < 7true
<=less than or equal3 <= 7true
>greater than3 > 7false
>=greater than or equal3 >= 7false
Conditional Expression(condition) ? value1 : value2if condition is true then value1 else value2true ? 3 : 73

ไม่มีความคิดเห็น:

แสดงความคิดเห็น