Friday, 10 December 2010

Mathematical Operators

Javascript Mathematical Operators

Given that y=5

Operator Description Example Result
+ Addition x=y+2 x=7
- Subtraction x=y-2 x=3
* Multiplication x=y*2 x=10
/ Division x=y/2 x=2.5
% Modulus(division remainder) x=y%2 x=1
++ Increment x=++y x=6
-- Decrement x=--y x=4
- Negation x=-y X=-5

In a compound equation like the one assigned to longEquation, each of the operations is subject to standard mathematical precedence (that is, multiplication and division operations are calculated first, from left to right, after which the addition and subtraction operations are calculated from left to right).

If you want to override the standard precedence of these operations, you can use brackets, just like you learned in school. Any operations that occur inside brackets will be calculated before any multiplication or division is done:

var unbracketed = 4 + 6 * 5;
var bracketed = (4 + 6) * 5;

Here, the value of unbracketed will be 34, because 6 * 5 is calculated first. The value of bracketed will be 50, because (4 + 6) is calculated first.

Operators Precedence

Javascript Operators Precedence

Operator
Description
Priority
. [] member Highest
() call  
! ~ - + ++ -- negation/increment typeof void delete  
* / % Multiplication, Division, Modulus  
+ - Addition, Subtraction  
<< >> >>> Bitwise Shift  
< <= > >= Comparison  
== != Equality  
& bitwise-and  
^ bitwise-xor  
| bitwise-or  
&& Logical AND  
|| Logical OR  
?: Conditional  
= += -= *= /= %= Assignment  
, Comma Lowest

Comparison Operators

Javascript Comparison Operators

Operator Description Example
== Equals x == 6 returns true if x is 6
!= Does not equal x != 6 returns true if x is not 6
< Less-than operator. x < 6 returns true if x is less than 6
> Greater-than operator x > 6 returns true if x is greater than 6
>= Greater-than or equal operator x >= 6 returns true if x is either 6, or greater than 6
<= Is less than or equal to x <= 6 returns true if x is either equal to 6, or less than 6

String operators

Javascript String operators

If you’d like to add two strings together, or concatenate them, you use the same + operator that you use for numbers:

var complete = "com" + "plete";

The value of complete will now be "complete".Again, you can use a combination of strings and string variables with the + operator:

var name = "Slim Shady";
var sentence = "Your name is " + name;

The value of sentence will be "Your name is Slim Shady". You can use the += operator with strings, but not the ++ operator—it doesn’t make sense to increment strings. So the previous set of statements could be rewritten as:

var name = "Slim Shady";
var sentence = "Your name is ";
sentence += name;

If you try to add a number to a string, JavaScript will automatically convert the number into a string, then concatenate the two resulting strings:

var sentence = "I am " + 1977
sentence now contains " I am 1977".

Assignment Operators

JavaScript Assignment Operators

Assignment operators are used to assign values to JavaScript variables.

Given that x=20 and y=10

Operator Example Same As Result
= x=y  
x=10
+= x+=y x=x+y x=30
-= x-=y x=x-y x=10
*= x*=y x=x*y x=200
/= x/=y x=x/y x=2
%= x%=y x=x%y x=0