Operators and Expressions
Operators and Expressions
Statements
• We have been using statements to
execute our JavaScript code
• Statements often have expressions
• Expressions produce values
INTERACTIVITY
01.06 Operators and Expressions WITH JAVASCRIPT
Expression
• So if you think back to LHS = RHS, the
LHS is a variable and the RHS is what
generates the value
• What are our tools for generating values
on the RHS?
INTERACTIVITY
01.06 Operators and Expressions WITH JAVASCRIPT
Assignment Operators
Operator Example Value stored in x
= x=5 5
y = 12
=
x=y 12
INTERACTIVITY
01.06 Operators and Expressions WITH JAVASCRIPT
Arithmetic Operators
Operator Example Value stored in x
+ x=2+5 7
- x=5-2 3
* x=2*5 10
/ x = 5/2 2.5
% x = 5%2 1
INTERACTIVITY
01.06 Operators and Expressions WITH JAVASCRIPT
More Operators
Operator Example Value stored in x
x = 5; 6
++
x++;
x = 12; 11
--
x--
x = 2; 7
+=
x+=5
INTERACTIVITY
01.06 Operators and Expressions WITH JAVASCRIPT
String Operators
Boolean Operators
• We can also use operators to compare values
• Assume x = 12;
Operator Example Returns
== x ==5 false
== x ==12 true
!= x !=5 true
INTERACTIVITY
01.06 Operators and Expressions WITH JAVASCRIPT
Boolean Operators
• Assume x = 12;
Boolean Operators
• Assume x = 12;
Operator Example Returns…
== x == “12” true
=== x === “12” false
!=== x !== 12 false
Logical Operators
• Assume x = 12;
Operator Example returns…
(15 > x) && (x > 5) true
&&
both sides must be true
(15 > x) || (x > 5) true
||
at least one side must be true
! !(x == 12) false
INTERACTIVITY
01.06 Operators and Expressions WITH JAVASCRIPT
Review
• Programming is not just about knowing
the syntax of a language
• You need to think about the logic behind
what you want to do, before you start to
code
INTERACTIVITY
01.06 Operators and Expressions WITH JAVASCRIPT
Acknowledgements/Contributions
These slides are Copyright 2015- Colleen van Lent as part of
http://www.intro-webdesign.com/ and made available under a Creative
Commons Attribution Non-Commercial 4.0 License. Please maintain
this last slide in all copies of the document to comply with the attribution
requirements of the license. If you make a change, feel free to add
your name and organization to the list of contributors on this page as
you republish the materials.