JavaScript syntax: Difference between revisions

Content deleted Content added
add "Miscellaneous" section, rename "Exception handling" section
Operators: add content
Line 59:
 
==Operators==
 
===Arithmetic operators===
The '+' operator is [[operator overloading|overloaded]]; it is used for string concatenation and arithmetic addition and also to convert strings to numbers (not to mention that it has special meaning when used in a [[regular expression]]).
 
Line 81 ⟶ 79:
alert( +z + x) // displays 6
 
===Conditional operatorArithmetic===
Binary operators
Also known as the [[?:|ternary operator]]
<pre>
+ Addition
- Subtraction
* Multiplication
/ Division (returns a floating-point value)
% Modulus (returns the integer remainder)
</pre>
 
Unary operators
<pre>
- Unary negation (reverses the sign)
++ Increment (can be prefix or postfix)
-- Decrement (can be prefix or postfix)
</pre>
 
===Assignment===
<pre>
= Assign
 
+= Add and assign
-= Subtract and assign
*= Multiply and assign
/= Divide and assign
%= Modulus and assign
 
&= Bitwise AND and assign
|= Bitwise OR and assign
^= Bitwise XOR and assign
 
<<= Left shift (zero fill) and assign
>>= Right shift (sign-propogating) and assign
>>>= Right shift (zero fill) and assign
</pre>
 
===Comparison===
<pre>
== Equal
!= Not equal
> Greater than
>= Greather than or equal to
< Less than
<= Less than or equal to
 
=== Identical (equal and of the same type)
!== Not identical
</pre>
 
===Conditional===
<pre>
? : Ternary comparison operator
 
condition ? val1 : val2 (Evaluates val1 if condition is true; otherwise, evaluates val2)
</pre>
 
===Boolean===
*Short-circuit logical operations
*Evaluates the minimal number of expressions necessary
*Partial evaluation (rather than full evaluation)
 
<pre>
&& and
|| or
! not (logical negation)
</pre>
 
*Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value; however, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.
 
===Bitwise===
Binary operators
<pre>
& And
| Or
^ Xor
 
<< Shift left (zero fill)
>> Shift right (sign-propogating); copies of the leftmost bit (sign bit) are shifted in from the left.
>>> Shift right (zero fill)
 
For positive numbers, >> and >>> yield the same result.
</pre>
 
Unary operators
<pre>
~ Not (inverts the bits)
</pre>
 
===String===
<pre>
= Assignment
+ Concatenation
+= Concatenate and assign
</pre>
 
Examples
 
<pre>
condition ? statement : statement;
str = "ab" + "cd"; // "abcd"
str += "e"; // "abcde"
</pre>
 
==Control structures==