SlideShare a Scribd company logo
JavaScript
Operators
Charles Russell
Bennu Bird Media
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
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
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
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.
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
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
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
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 ++
Multiplication
●
*
●
Binary operator
●
Returns NaN if an operand is a non numeric string
Division
●
/
●
Binary operator
●
Behaves in a simular fashion to Multiplication
Modulus
●
%
●
Binary operator
●
Does integer divsion and returns the remainder
●
Again type conversion is attempted
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
The Rest of the
Assignment Family
●
+=, -=, *=, %=
●
These operators do a calculation and assign the
value simutaneously
●
Example
●
A=5; A+=3
– Result 8
Comparison
●
These return a boolean
●
Equality ==
●
Super Equality ===
●
Inequality !=
●
Super Inequality !==
●
Less than <
●
Less than or Equal <=
●
Greater Than >
●
Greater than or Equal to >=
Next: More Operators
JavaScript
Operators part2
Charles Russell
Bennu Bird Media
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.
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 Result
T T B
T F B
F F A
F T A
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
Not !
●
Reverses a Boolean value
Bit opeations
●
& bitwise and
●
| bitwise or
●
^ bitwise xor
●
~ bitwise not
●
<< shift left
●
>> shift right
●
>>> shift right zero fill
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/bi
●
What you can do with this
●
http://michalbe.blogspot.com/2013/03/javascript-less-kno
●
http://graphics.stanford.edu/~seander/bithacks.html
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
Grouping
●
()
●
Groups expressions that need to be evaluated
first
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
Operator Precedence
[] ()
++ -- - ~ !
* / %
+ - +
<< >> >>>
< <= > >=
== != === !==
&
^
|
&&
||
? :
= += -= *= ...

More Related Content

What's hot (20)

PDF
Client sidescripting javascript
Selvin Josy Bai Somu
 
PDF
Ruby Functional Programming
Geison Goes
 
PDF
Scala - core features
Łukasz Wójcik
 
PPT
Type conversions
sanya6900
 
PPTX
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
reactima
 
PDF
Functional JavaScript Fundamentals
Srdjan Strbanovic
 
PPTX
Functional Programming in JavaScript by Luis Atencio
Luis Atencio
 
ODP
Functional programming
S M Asaduzzaman
 
PDF
Angular 2.0: Brighter future?
Eugene Zharkov
 
PDF
LC2018 - Input Validation in PureScript
Joe Kachmar
 
PDF
Intro to JavaScript - Week 2: Function
Jeongbae Oh
 
PDF
JavaScript Functions
Colin DeCarlo
 
PPTX
Functional programming and ruby in functional style
Niranjan Sarade
 
PPT
C++ overloading
sanya6900
 
PPTX
2CPP03 - Object Orientation Fundamentals
Michael Heron
 
PDF
Let's JavaScript
Paweł Dorofiejczyk
 
PDF
Introduction to Object Oriented Javascript
nodeninjas
 
PDF
Reactive Programming with JavaScript
Codemotion
 
PPT
Operator overloading
ArunaDevi63
 
PPTX
Presentation on overloading
Charndeep Sekhon
 
Client sidescripting javascript
Selvin Josy Bai Somu
 
Ruby Functional Programming
Geison Goes
 
Scala - core features
Łukasz Wójcik
 
Type conversions
sanya6900
 
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
reactima
 
Functional JavaScript Fundamentals
Srdjan Strbanovic
 
Functional Programming in JavaScript by Luis Atencio
Luis Atencio
 
Functional programming
S M Asaduzzaman
 
Angular 2.0: Brighter future?
Eugene Zharkov
 
LC2018 - Input Validation in PureScript
Joe Kachmar
 
Intro to JavaScript - Week 2: Function
Jeongbae Oh
 
JavaScript Functions
Colin DeCarlo
 
Functional programming and ruby in functional style
Niranjan Sarade
 
C++ overloading
sanya6900
 
2CPP03 - Object Orientation Fundamentals
Michael Heron
 
Let's JavaScript
Paweł Dorofiejczyk
 
Introduction to Object Oriented Javascript
nodeninjas
 
Reactive Programming with JavaScript
Codemotion
 
Operator overloading
ArunaDevi63
 
Presentation on overloading
Charndeep Sekhon
 

Similar to JavaScript Operators (20)

PPTX
datatypes-and-operators in wed development.pptx
FahimMousa
 
PPTX
Operator 04 (js)
AbhishekMondal42
 
ODP
Operators
jayesh30sikchi
 
PPTX
Java script session 4
Saif Ullah Dar
 
PPTX
Operators Concept in Python-N.Kavitha.pptx
Kavitha713564
 
PPT
Operators
Kamran
 
PPTX
Opeartor &amp; expression
V.V.Vanniapermal College for Women
 
PPTX
Operators used in vb.net
Jaya Kumari
 
PPTX
Operator in c programming
Manoj Tyagi
 
PPTX
Chapter 1 .pptx
MohamedAbdullahiYusu
 
PDF
Lec13
Sri Harsha Pamu
 
PPT
Java 2
Preethi Nambiar
 
PPT
Java - Operators
Preethi Nambiar
 
PPTX
Session03 operators
HarithaRanasinghe
 
PPTX
Operators expressions-and-statements
CtOlaf
 
PPT
03 Operators and expressions
maznabili
 
PDF
itft-Operators in java
Atul Sehdev
 
PPTX
Javascript operators
Mohit Rana
 
PPT
C Sharp Jn (2)
guest58c84c
 
PPT
C Sharp Jn (2)
jahanullah
 
datatypes-and-operators in wed development.pptx
FahimMousa
 
Operator 04 (js)
AbhishekMondal42
 
Operators
jayesh30sikchi
 
Java script session 4
Saif Ullah Dar
 
Operators Concept in Python-N.Kavitha.pptx
Kavitha713564
 
Operators
Kamran
 
Opeartor &amp; expression
V.V.Vanniapermal College for Women
 
Operators used in vb.net
Jaya Kumari
 
Operator in c programming
Manoj Tyagi
 
Chapter 1 .pptx
MohamedAbdullahiYusu
 
Java - Operators
Preethi Nambiar
 
Session03 operators
HarithaRanasinghe
 
Operators expressions-and-statements
CtOlaf
 
03 Operators and expressions
maznabili
 
itft-Operators in java
Atul Sehdev
 
Javascript operators
Mohit Rana
 
C Sharp Jn (2)
guest58c84c
 
C Sharp Jn (2)
jahanullah
 
Ad

More from Charles Russell (9)

PPT
Js objects
Charles Russell
 
PPT
JavaScript Introductin to Functions
Charles Russell
 
PPT
JavaScript iteration
Charles Russell
 
PPT
Open Source Software
Charles Russell
 
PPT
JavaScript Variables
Charles Russell
 
PPT
JavaScript Tools and Implementation
Charles Russell
 
PPT
JavaScript Comments and Conditionals
Charles Russell
 
PPT
JavaScript Introduction
Charles Russell
 
PPTX
Extending drupal authentication
Charles Russell
 
Js objects
Charles Russell
 
JavaScript Introductin to Functions
Charles Russell
 
JavaScript iteration
Charles Russell
 
Open Source Software
Charles Russell
 
JavaScript Variables
Charles Russell
 
JavaScript Tools and Implementation
Charles Russell
 
JavaScript Comments and Conditionals
Charles Russell
 
JavaScript Introduction
Charles Russell
 
Extending drupal authentication
Charles Russell
 
Ad

Recently uploaded (20)

PPTX
Presentation3gsgsgsgsdfgadgsfgfgsfgagsfgsfgzfdgsdgs.pptx
SUB03
 
PPTX
PE introd.pptxfrgfgfdgfdgfgrtretrt44t444
nepmithibai2024
 
PPTX
ONLINE BIRTH CERTIFICATE APPLICATION SYSYTEM PPT.pptx
ShyamasreeDutta
 
PPTX
04 Output 1 Instruments & Tools (3).pptx
GEDYIONGebre
 
PPTX
Optimization_Techniques_ML_Presentation.pptx
farispalayi
 
PPTX
L1A Season 1 ENGLISH made by A hegy fixed
toszolder91
 
PPT
Agilent Optoelectronic Solutions for Mobile Application
andreashenniger2
 
PPTX
sajflsajfljsdfljslfjslfsdfas;fdsfksadfjlsdflkjslgfs;lfjlsajfl;sajfasfd.pptx
theknightme
 
PPT
introduction to networking with basics coverage
RamananMuthukrishnan
 
PDF
Apple_Environmental_Progress_Report_2025.pdf
yiukwong
 
PPTX
法国巴黎第二大学本科毕业证{Paris 2学费发票Paris 2成绩单}办理方法
Taqyea
 
PDF
Azure_DevOps introduction for CI/CD and Agile
henrymails
 
PPTX
Softuni - Psychology of entrepreneurship
Kalin Karakehayov
 
PPT
introductio to computers by arthur janry
RamananMuthukrishnan
 
PPTX
PM200.pptxghjgfhjghjghjghjghjghjghjghjghjghj
breadpaan921
 
PPTX
原版西班牙莱昂大学毕业证(León毕业证书)如何办理
Taqyea
 
PDF
BRKACI-1001 - Your First 7 Days of ACI.pdf
fcesargonca
 
PPTX
L1A Season 1 Guide made by A hegy Eng Grammar fixed
toszolder91
 
PDF
AI_MOD_1.pdf artificial intelligence notes
shreyarrce
 
PDF
𝐁𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓
hokimamad0
 
Presentation3gsgsgsgsdfgadgsfgfgsfgagsfgsfgzfdgsdgs.pptx
SUB03
 
PE introd.pptxfrgfgfdgfdgfgrtretrt44t444
nepmithibai2024
 
ONLINE BIRTH CERTIFICATE APPLICATION SYSYTEM PPT.pptx
ShyamasreeDutta
 
04 Output 1 Instruments & Tools (3).pptx
GEDYIONGebre
 
Optimization_Techniques_ML_Presentation.pptx
farispalayi
 
L1A Season 1 ENGLISH made by A hegy fixed
toszolder91
 
Agilent Optoelectronic Solutions for Mobile Application
andreashenniger2
 
sajflsajfljsdfljslfjslfsdfas;fdsfksadfjlsdflkjslgfs;lfjlsajfl;sajfasfd.pptx
theknightme
 
introduction to networking with basics coverage
RamananMuthukrishnan
 
Apple_Environmental_Progress_Report_2025.pdf
yiukwong
 
法国巴黎第二大学本科毕业证{Paris 2学费发票Paris 2成绩单}办理方法
Taqyea
 
Azure_DevOps introduction for CI/CD and Agile
henrymails
 
Softuni - Psychology of entrepreneurship
Kalin Karakehayov
 
introductio to computers by arthur janry
RamananMuthukrishnan
 
PM200.pptxghjgfhjghjghjghjghjghjghjghjghjghj
breadpaan921
 
原版西班牙莱昂大学毕业证(León毕业证书)如何办理
Taqyea
 
BRKACI-1001 - Your First 7 Days of ACI.pdf
fcesargonca
 
L1A Season 1 Guide made by A hegy Eng Grammar fixed
toszolder91
 
AI_MOD_1.pdf artificial intelligence notes
shreyarrce
 
𝐁𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓
hokimamad0
 

JavaScript Operators

  • 2. 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
  • 3. 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
  • 4. 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
  • 5. 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.
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. 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 ++
  • 10. Multiplication ● * ● Binary operator ● Returns NaN if an operand is a non numeric string
  • 11. Division ● / ● Binary operator ● Behaves in a simular fashion to Multiplication
  • 12. Modulus ● % ● Binary operator ● Does integer divsion and returns the remainder ● Again type conversion is attempted
  • 13. 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
  • 14. The Rest of the Assignment Family ● +=, -=, *=, %= ● These operators do a calculation and assign the value simutaneously ● Example ● A=5; A+=3 – Result 8
  • 15. Comparison ● These return a boolean ● Equality == ● Super Equality === ● Inequality != ● Super Inequality !== ● Less than < ● Less than or Equal <= ● Greater Than > ● Greater than or Equal to >=
  • 18. 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.
  • 19. 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 Result T T B T F B F F A F T A
  • 20. 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
  • 21. Not ! ● Reverses a Boolean value
  • 22. Bit opeations ● & bitwise and ● | bitwise or ● ^ bitwise xor ● ~ bitwise not ● << shift left ● >> shift right ● >>> shift right zero fill
  • 23. 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/bi ● What you can do with this ● http://michalbe.blogspot.com/2013/03/javascript-less-kno ● http://graphics.stanford.edu/~seander/bithacks.html
  • 24. 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
  • 25. Grouping ● () ● Groups expressions that need to be evaluated first
  • 26. 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
  • 27. Operator Precedence [] () ++ -- - ~ ! * / % + - + << >> >>> < <= > >= == != === !== & ^ | && || ? : = += -= *= ...