javascript operators

27
JavaScript Operators Charles Russell Bennu Bird Media

Upload: charles-russell

Post on 22-May-2015

420 views

Category:

Internet


20 download

DESCRIPTION

This is the slide stack to the two JavaScript Operators YouTube videos at https://www.youtube.com/watch?v=4sF-9RqDxEA and https://www.youtube.com/watch?v=lRijlc3tsw0

TRANSCRIPT

Page 1: JavaScript Operators

JavaScript Operators

Charles Russell

Bennu Bird Media

Page 2: JavaScript Operators

What is an Operator

● An Operator is a function with different syntax● Operators performs some action and returns a value● The values being acted upon are called operands● The value returned is the result● Operands may be literals, variables, or expressions

● They are used to make expressions

Page 3: JavaScript Operators

Operators can be catagorized by size

● An expression can consist of many operators but when broken down to each operation idvidually, there are three sizes● A unary operator is an operator on a single value

– The – sign makes a number negative● A binary operator is an operator works with two values

– Your normal addition, subtraction, multiplication and division● Ternary Operator that takes three values

Page 4: JavaScript Operators

The + Operator

● It is both a unary and binary operator● Unary operation, + on a line in front of a variable

and it makes that variable a number.● It tries to make sense of the operand. This is called

coercion If the string contains only digits then you get the value contained in the string, a boolean returns 1 for true 0 for false

● If it can not make a number that makes sense a NaN is returned ex: any letters in the string

Page 5: JavaScript Operators

The + operator in binary mode

● Numbers is not all this operator works on● This is also the concatenation operator for strings.

● The various combinations of number and string resolve to different data types with this language● number + number = number● string + string = string● string + number = string

● Number () or String() to type cast as needed.

Page 6: JavaScript Operators

Subtraction or Minus

● The – operator is again both binary and unary ● Returns a number no matter the mode● A-B

● The – operator returns A reduced by the value of B– If the operands can be logically turned to numbers the result

will be the value of the operations otherwise you get NaN

Page 7: JavaScript Operators

The – operator in Unary mode

For numeric value changes the sign (pos to neg) (neg to pos)

● Tries to make a number out of other data types

Page 8: JavaScript Operators

Introducing the increment

● ++ unary operator that adds 1 to the current value● Pre increment post increment.

● Although in the end the variable is incremented the side of the variable you are incrementing that you put the operater on matters

● If the operator comes before the variable (pre increment) the operation occures then assignment

● If the operator comes second assignment happens then the increment

● Strings will try to be converted to numbers non numeric strings will return NaN

Page 9: JavaScript Operators

Decrement

● If there is an increment operator then logically there must be a decrement operator

● As you would suspect - -● Behaves in a simular fashion to ++

Page 10: JavaScript Operators

Multiplication

● *● Binary operator● Returns NaN if an operand is a non numeric string

Page 11: JavaScript Operators

Division

● /● Binary operator● Behaves in a simular fashion to Multiplication

Page 12: JavaScript Operators

Modulus

● %● Binary operator● Does integer divsion and returns the remainder● Again type conversion is attempted

Page 13: JavaScript Operators

Assignment

● =● Assigns a value to a variable

● Lvalue assigned to rvalue● Not a check for equality that is another operator

● X=5 Works fine ● 5=5 'X' = 5 or 'X' = 'X' results in a reference error

Page 14: JavaScript Operators

The Rest of the Assignment Family

● +=, -=, *=, %=● These operators do a calculation and assign the

value simutaneously● Example

● A=5; A+=3– Result 8

Page 15: JavaScript Operators

Comparison

● These return a boolean● Equality ==● Super Equality ===● Inequality !=● Super Inequality !==● Less than <

● Less than or Equal <=● Greater Than >

● Greater than or Equal to >=

Page 16: JavaScript Operators

Next: More Operators

Page 17: JavaScript Operators

JavaScript Operators part2

Charles Russell

Bennu Bird Media

Page 18: JavaScript Operators

Whats the truth

● I'm not strange, weird, off, nor crazy, my reality is just different from yours (Lewis Carol)

● You would expect && and || to return boolean but it may or may not● Shortcut evaluation

– Evaluation of is done from left to right value returned as soon as evaluation can be made

– Remember truthy and falsey● The truthy or falseyness of any value can be determined● This means that a value that is not boolean can be returned and the

truthynesss or falsyness of the expression can be evaluated.

Page 19: JavaScript Operators

And &&

● A && B ● True Only if both opeands are true● If A falsey then B returned Data Type unchanged● Used to Gaurd B

A B ResultT T B

T F B

F F A

F T A

Page 20: JavaScript Operators

Or ||

● A || B ● If A False B is returned data type unchanged● True if A truthy or B Truthy● Very useful to check if a value is set

A B Result

T T A

T F A

F T B

F T B

Page 21: JavaScript Operators

Not !

● Reverses a Boolean value

Page 22: JavaScript Operators

Bit opeations

● & bitwise and● | bitwise or● ^ bitwise xor● ~ bitwise not● << shift left● >> shift right● >>> shift right zero fill

Page 23: JavaScript Operators

Bit twiddling used to something to avoid

● This was the convential wisdom.● While this improved effiency in other languages in this

one performance degraded● This seems to be changing

● http://dreaminginjavascript.wordpress.com/2009/02/09/bitwise-byte-foolish/

● What you can do with this● http://michalbe.blogspot.com/2013/03/javascript-less-k

nown-parts-bitwise.html● http://graphics.stanford.edu/~seander/bithacks.html

Page 24: JavaScript Operators

The conditional operator

● This is the tenary operator and it roughly equates to if operand 1 then operand 2 else operand 3

● Operand1 ? Operand2 : Operand3● Operand 1 is the boolean if true

– Do the action specified in Operand 2● Else do the action in specified operand 3

Page 25: JavaScript Operators

Grouping

● ()● Groups expressions that need to be evaluated

first

Page 26: JavaScript Operators

Yes there are others

● I am not mentioning some operators here because many of these not mentioned apply to functions, arrays and Objects topics yet to be covered these other operaterators will be introduced and disscussed when and if appropriate

Page 27: JavaScript Operators

Operator Precedence

[] ()

++ -- - ~ !

* / %

+ - +

<< >> >>>

< <= > >=

== != === !==

&

^

|

&&

||

? :

= += -= *= ...