Jun 10, 2015
JAVASCRIPTOPERATORSVictor Perez
/ TO PRIMITIVE
⁄ ⁄ ⁄
● Preferred type, string or number
○ if no preferred type, default number except for instances of Date
CONVERSIONTO PRIMITIVE
⁄ ⁄ ⁄
● plus ( + )
● comparison ( <, <=, >, >=, ==, != )
○ in- and equality only if both aren't a object
PLUS AND COMPARISON TO PRIMITIVE
/ OPERATORS
⁄ ⁄ ⁄
● Organized by operator precedence
● Punctuation characters
○ like: +, =, %
● Keywords
○ like: delete, void, typeof
● Associativity
○ LTR ( Left-to-right ) and RTL ( Right-to-left )
● Number of operands
○ 1 - 3
● Types
○ input type(s) → result type
INTRODUCTIONOPERATORS
⁄ ⁄ ⁄
INCREMENTOPERATORS
● Pre- or post-increment
Operator Associativity Number of operands Types
++ Right-to-left 1 lvalue → number
⁄ ⁄ ⁄
DECREMENTOPERATORS
● Pre- or post-decrement
Operator Associativity Number of operands Types
-- Right-to-left 1 lvalue → number
⁄ ⁄ ⁄
UNARY MINUSOPERATORS
● Changes the sign of the result
Operator Associativity Number of operands Types
- Right-to-left 1 number → number
⁄ ⁄ ⁄
UNARY PLUSOPERATORS
● Converts to a number
● See type conversion last presentation http://www.slideshare.net/victorandresperez/java-script-introduction-34306832/26
Operator Associativity Number of operands Types
+ Right-to-left 1 number → number
⁄ ⁄ ⁄
BITWISE NOTOPERATORS
● Reversing all bits
● -( x + 1)
Operator Associativity Number of operands Types
~ Right-to-left 1 int → int
⁄ ⁄ ⁄
DELETEOPERATORS
● Removes a property
○ returns true if the property was successful deleted
Operator Associativity Number of operands Types
delete Right-to-left 1 lvalue→ boolean
⁄ ⁄ ⁄
TYPEOFOPERATORS
● Determine type of operand
○ http://www.slideshare.net/victorandresperez/java-script-introduction-34306832/25
Operator Associativity Number of operands Types
typeof Right-to-left 1 any → string
⁄ ⁄ ⁄
VOIDOPERATORS
● Evaluates its operand and then returns undefined
Operator Associativity Number of operands Types
void Right-to-left 1 any → undefined
⁄ ⁄ ⁄
MULTIPLY, DIVIDE & REMAINDEROPERATORS
● Multiplication
● Division
● Modulo ( remainder after division )
Operator Associativity Number of operands Types
* Left-to-right 2 number, number → number
/ Left-to-right 2 number, number → number
% Left-to-right 2 number, number → number
⁄ ⁄ ⁄
ADD & SUBTRACTOPERATORS
● Addition ( + )
● Subtraction ( - )
Operator Associativity Number of operands Types
+ Left-to-right 2 number, number → number
- Left-to-right 2 number, number → number
⁄ ⁄ ⁄
CONCATENATE STRINGSOPERATORS
● Will concatenate both strings to a new string
Operator Associativity Number of operands Types
+ Left-to-right 2 string,string→ string
⁄ ⁄ ⁄
SHIFT LEFTOPERATORS
● moves all bits in its first operand to the left by the number of places in the second operand
● between 0 and 31
● x * 2^y
Operator Associativity Number of operands Types
<< Left-to-right 2 int, int→ int
⁄ ⁄ ⁄
SHIFT RIGHT WITH SIGNOPERATORS
● moves all bits in its first operand to the right by the number of places specified in the second operand
● between 0 and 31
Operator Associativity Number of operands Types
>> Left-to-right 2 int, int→ int
⁄ ⁄ ⁄
SHIFT RIGHT WITH ZERO FILLOPERATORS
● Same as >> operator, except that the bits shifted in on the left are always zero
Operator Associativity Number of operands Types
>>> Left-to-right 2 int, int→ int
⁄ ⁄ ⁄
COMPARE NUMBERSOPERATORS
● Less than ( < ), greater than ( > ), less than or equal ( <= ), greater than or equal ( >= )
Operator Associativity Number of operands Types
< Left-to-right 2 number, number→ boolean
> Left-to-right 2 number, number→ boolean
<= Left-to-right 2 number, number→ boolean
>= Left-to-right 2 number, number→ boolean
⁄ ⁄ ⁄
COMPARE STRINGSOPERATORS
● Less than ( < ), greater than ( > ), less than or equal ( <= ), greater than or equal ( >= )
● 16-bit integers
● Capital ASCII is less than lowercase ASCII
Operator Associativity Number of operands Types
< Left-to-right 2 string, string → boolean
> Left-to-right 2 string, string → boolean
<= Left-to-right 2 string, string → boolean
>= Left-to-right 2 string, string → boolean
⁄ ⁄ ⁄
INSTANCEOFOPERATORS
● Checks of the left operand is an instanceof the right operand
Operator Associativity Number of operands Types
instanceof Left-to-right 2 object,function→ boolean
⁄ ⁄ ⁄
INOPERATORS
● Checks of the left-side value is a property in the right-side value
Operator Associativity Number of operands Types
in Left-to-right 2 string,object→ boolean
⁄ ⁄ ⁄
EQUALITYOPERATORS
● Checks of both operands are equal
● 2 objects are check by references
● Type conversion if types are not equal
Operator Associativity Number of operands Types
== Left-to-right 2 any,any→ boolean
⁄ ⁄ ⁄
INEQUALITYOPERATORS
● Checks of both operands are not equal
● 2 objects are check by references
● Type conversion if types are not equal
Operator Associativity Number of operands Types
!= Left-to-right 2 any,any→ boolean
⁄ ⁄ ⁄
STRICT EQUALITYOPERATORS
● Checks of both operands are equal
● 2 objects are check by references
Operator Associativity Number of operands Types
=== Left-to-right 2 any,any→ boolean
⁄ ⁄ ⁄
STRICT INEQUALITYOPERATORS
● Checks of both operands are not equal
● 2 objects are check by references
Operator Associativity Number of operands Types
!== Left-to-right 2 any,any→ boolean
⁄ ⁄ ⁄
BITWISE ANDOPERATORS
● Performs the AND operation on each pair of bits
○ yields 1 if both bits are 1, else yields 0
Operator Associativity Number of operands Types
& Left-to-right 2 int,int→ int
⁄ ⁄ ⁄
BITWISE XOROPERATORS
● Performs the XOR operation on each pair of bits
○ yields 1 if both bits not equal, else yields 0
Operator Associativity Number of operands Types
^ Left-to-right 2 int,int→ int
⁄ ⁄ ⁄
BITWISE OROPERATORS
● Performs the OR operation on each pair of bits
○ yields 0 if both bits are 0, else yields 1
Operator Associativity Number of operands Types
| Left-to-right 2 int,int→ int
⁄ ⁄ ⁄
LOGICAL ANDOPERATORS
● Returns a “truthy” or “falsy” value
● Returns the left operand if the left operand is “falsy”, else return right operand
● “falsy” values are: false, null, undefined, 0, -0, NaN and empty string ( "" )
Operator Associativity Number of operands Types
&& Left-to-right 2 any,any→ any
⁄ ⁄ ⁄
LOGICAL OROPERATORS
● Returns a “truthy” or “falsy” value
● Returns the left operand if the left operand is “truthy”, else return right operand
● “falsy” values are: false, null, undefined, 0, -0, NaN and empty string ( "" )
Operator Associativity Number of operands Types
|| Left-to-right 2 any,any→ any
⁄ ⁄ ⁄
CONDITIONAL OPERATOROPERATORS
● The only ternary operator ( three operands )
● if first operand is “truly”, then the second operand is evaluated, else the third operand
Operator Associativity Number of operands Types
?: Right-to-left 3 boolean,any,any→ any
⁄ ⁄ ⁄
ASSIGNMENTOPERATORS
● Assign to a variable or property
Operator Associativity Number of operands Types
= Right-to-left 2 lvalue,any→ any
⁄ ⁄ ⁄
OPERATE AND ASSIGNOPERATORS
● a operate= b is the same as a = a operate b
Operators Associativity Number of operands Types
*=, /=, %=, +=, -=, &=, ^=, |=, <<=, >>=, >>>=
Right-to-left 2 lvalue,any→ any
Operator Example Equivalent
+=-=*=/=%=<<=>>=
>>>=&=|=^=
a += ba -= ba *= ba /= ba %= ba <<= ba >>= ba >>>= ba &= ba |= ba ^= b
a = a + ba = a - ba = a * ba = a / ba = a % ba = a << ba = a >> ba = a >>> ba = a & ba = a | ba = a ^ b
⁄ ⁄ ⁄
COMMAOPERATORS
● Evaluates the left operand, then the right operand and then returns the value of the right operand.
Operators Associativity Number of operands Types
, Left-to-right 2 any,any→ any
/ QUESTIONS?
THANKS@[email protected]