04-Introduction-To-JavaScript
04-Introduction-To-JavaScript
Eric Roberts
CS 106J
April 10, 2017
The Karel Contest
The CS 106A
Karel
Contest
JavaScript Console
-> 2 + 2
4
-> 342 - 173
169
-> 12345679 * 63
777777777
-> 9 * 9 * 9 + 10 * 10 * 10
1729
->
Variables
• The simplest terms that appear in expressions are constant
literals and variables. A variable is a placeholder for a value
that can be updated as the program runs.
• A variable in JavaScript is most easily envisioned as a box
capable of storing a value
answer
42
* / %
+ -
lowest
Thus, JavaScript evaluates unary – operators first, then the
operators *, /, and %, and then the operators + and -.
• Precedence applies only when two operands compete for the
same operator. If the operators are independent, JavaScript
evaluates expressions from left to right. Parentheses may be
used to change the order of operations.
Exercise: Precedence Evaluation
What is the value of the expression at the bottom of the screen?
42
51
45
6 3
2 9 15
1 * 2 * 3 + (4 + 5) % 6 * (7 + 8) - 9
Assignment Statements
• You can change the value of a variable in your program by
using an assignment statement, which has the general form:
variable = expression;
return expression;
function fahrenheitToCelsius(f) {
return 5 / 9 * (f – 32);
}
function quotient(x, y) {
return Math.floor(x / y);
}