40

JavaScript operators

Embed Size (px)

DESCRIPTION

JavaScript presentation for 24/i, that will touch all JavaScript operators

Citation preview

Page 1: JavaScript operators
Page 2: JavaScript operators

JAVASCRIPTOPERATORSVictor Perez

Page 3: JavaScript operators

/ TO PRIMITIVE

Page 4: JavaScript operators

⁄ ⁄ ⁄

● Preferred type, string or number

○ if no preferred type, default number except for instances of Date

CONVERSIONTO PRIMITIVE

Page 5: JavaScript operators

⁄ ⁄ ⁄

● plus ( + )

● comparison ( <, <=, >, >=, ==, != )

○ in- and equality only if both aren't a object

PLUS AND COMPARISON TO PRIMITIVE

Page 6: JavaScript operators

/ OPERATORS

Page 7: JavaScript 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

Page 8: JavaScript operators

⁄ ⁄ ⁄

INCREMENTOPERATORS

● Pre- or post-increment

Operator Associativity Number of operands Types

++ Right-to-left 1 lvalue → number

Page 9: JavaScript operators

⁄ ⁄ ⁄

DECREMENTOPERATORS

● Pre- or post-decrement

Operator Associativity Number of operands Types

-- Right-to-left 1 lvalue → number

Page 10: JavaScript operators

⁄ ⁄ ⁄

UNARY MINUSOPERATORS

● Changes the sign of the result

Operator Associativity Number of operands Types

- Right-to-left 1 number → number

Page 11: JavaScript operators

⁄ ⁄ ⁄

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

Page 12: JavaScript operators

⁄ ⁄ ⁄

BITWISE NOTOPERATORS

● Reversing all bits

● -( x + 1)

Operator Associativity Number of operands Types

~ Right-to-left 1 int → int

Page 13: JavaScript operators

⁄ ⁄ ⁄

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

Page 14: JavaScript operators

⁄ ⁄ ⁄

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

Page 15: JavaScript operators

⁄ ⁄ ⁄

VOIDOPERATORS

● Evaluates its operand and then returns undefined

Operator Associativity Number of operands Types

void Right-to-left 1 any → undefined

Page 16: JavaScript operators

⁄ ⁄ ⁄

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

Page 17: JavaScript operators

⁄ ⁄ ⁄

ADD & SUBTRACTOPERATORS

● Addition ( + )

● Subtraction ( - )

Operator Associativity Number of operands Types

+ Left-to-right 2 number, number → number

- Left-to-right 2 number, number → number

Page 18: JavaScript operators

⁄ ⁄ ⁄

CONCATENATE STRINGSOPERATORS

● Will concatenate both strings to a new string

Operator Associativity Number of operands Types

+ Left-to-right 2 string,string→ string

Page 19: JavaScript operators

⁄ ⁄ ⁄

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

Page 20: JavaScript operators

⁄ ⁄ ⁄

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

Page 21: JavaScript operators

⁄ ⁄ ⁄

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

Page 22: JavaScript operators

⁄ ⁄ ⁄

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

Page 23: JavaScript operators

⁄ ⁄ ⁄

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

Page 24: JavaScript operators

⁄ ⁄ ⁄

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

Page 25: JavaScript operators

⁄ ⁄ ⁄

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

Page 26: JavaScript operators

⁄ ⁄ ⁄

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

Page 27: JavaScript operators

⁄ ⁄ ⁄

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

Page 28: JavaScript operators

⁄ ⁄ ⁄

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

Page 29: JavaScript operators

⁄ ⁄ ⁄

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

Page 30: JavaScript operators

⁄ ⁄ ⁄

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

Page 31: JavaScript operators

⁄ ⁄ ⁄

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

Page 32: JavaScript operators

⁄ ⁄ ⁄

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

Page 33: JavaScript operators

⁄ ⁄ ⁄

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

Page 34: JavaScript operators

⁄ ⁄ ⁄

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

Page 35: JavaScript operators

⁄ ⁄ ⁄

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

Page 36: JavaScript operators

⁄ ⁄ ⁄

ASSIGNMENTOPERATORS

● Assign to a variable or property

Operator Associativity Number of operands Types

= Right-to-left 2 lvalue,any→ any

Page 37: JavaScript operators

⁄ ⁄ ⁄

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

Page 38: JavaScript operators

⁄ ⁄ ⁄

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

Page 39: JavaScript operators

/ QUESTIONS?

Page 40: JavaScript operators

THANKS@[email protected]