Difference between revisions of "Javascript Operators"
From Elite Wiki
m (Added additional info about bitwise shifting operations) |
Cholmondely (talk | contribs) (Added link) |
||
(2 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
== Operators used in Javascript == | == Operators used in Javascript == | ||
− | Below is a listing of often used operators in javascript to help understand some of the used code in Oolite scripts. | + | Below is a listing of often used operators in javascript to help understand some of the used code in Oolite scripts. A quick introduction and further links can be found in [[Scripting_Oolite_with_JavaScript|Scripting Oolite with JavaScript]]. |
− | === | + | === Arithmetic Operators === |
'''Operator Description''' | '''Operator Description''' | ||
Line 48: | Line 48: | ||
| Bitwise or<br> | | Bitwise or<br> | ||
^ Bitwise exclusive or (xor)<br> | ^ Bitwise exclusive or (xor)<br> | ||
− | |||
− | |||
− | |||
− | |||
~ Flip all bits (invert 0 to 1 and 1 to 0)<br> | ~ Flip all bits (invert 0 to 1 and 1 to 0)<br> | ||
+ | Bitshifting operation converts to 32-bit integers, big-endian order and returns a result of the same type as the left operand.<br> | ||
+ | << Shift bits to the left (9 << 2 yields 36)<br> | ||
+ | >> Shift bits to the right, keep sign (9 >> 2 yields 2 BUT -9 >> 2 yields -3)<br> | ||
+ | >>> Shift bits to the right, discard sign bit (9 >>> 2 yields 2 BUT -9 >>> 2 yields 1073741821)<br> | ||
+ | |||
+ | === Oolite's Javascript === | ||
+ | See [[:Category:Oolite JavaScript Reference]] | ||
[[Category:Oolite scripting]] | [[Category:Oolite scripting]] |
Latest revision as of 19:58, 6 November 2021
Contents
Operators used in Javascript
Below is a listing of often used operators in javascript to help understand some of the used code in Oolite scripts. A quick introduction and further links can be found in Scripting Oolite with JavaScript.
Arithmetic Operators
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (remainder of a division)
++ Increment (++X means increment by one X before use and X++ means increment X after use
-- Decrement (--X means idecrement by one X before use and X-- means decrement X after use
Assignment Operators
Operator Description
= Assign
+= Add and assign. For example, x+=y is the same as x=x+y.
-= Subtract and assign. For example, x-=y is the same as x=x-y.
*= Multiply and assign. For example, x*=y is the same as x=x*y.
/= Divide and assign. For example, x/=y is the same as x=x/y.
%= Modulus and assign. For example, x%=y is the same as x=x%y.
Comparison Operators
Operator Description
== Is equal to
=== Is identical (is equal to and is of the same type)
!= Is not equal to
!== Is not identical
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
Logical/boolean Operators
Operator Description
&& and
|| or
! not
String Operators
In JavaScript, a string is simply a piece of text.
Operator Description
= Assignment
+ Concatenate (join two strings together)
+= Concatenate and assign
Bit Operators
Operator Description
& Bitwise and
| Bitwise or
^ Bitwise exclusive or (xor)
~ Flip all bits (invert 0 to 1 and 1 to 0)
Bitshifting operation converts to 32-bit integers, big-endian order and returns a result of the same type as the left operand.
<< Shift bits to the left (9 << 2 yields 36)
>> Shift bits to the right, keep sign (9 >> 2 yields 2 BUT -9 >> 2 yields -3)
>>> Shift bits to the right, discard sign bit (9 >>> 2 yields 2 BUT -9 >>> 2 yields 1073741821)