CS133JS Beginning Programming: JavaScript
Topics by Week | |
---|---|
1. Intro to JavaScript programming | 6. Arrays |
2. Functions, Variable scope, Operators and Expressions | 7. Functions |
3. Conditional Statements | 8. Objects |
4. Loops | 9. DOM |
5. Midterm | 10. Final |
ReviewMath OperatorsComparison OperatorsMore on Operators and ExpressionsCombined assignment and math operatorsThe range of numbers that can be representedRepresentation of infinityReference
JavaScript has all the math operators you know: +, -, /, * and one you might not: % (the modulo operator)
xxxxxxxxxx
var remainder = 8 % 3;
console.log(remainder);
There are increment and decrement operators:
x++
, x--
++x
, --x
xxxxxxxxxx
var count = 1;
count++
console.log(count);
console.log(count++);
console.log(count);
console.log(++count);
console.log(count);
Review: An expression is a unit of code that can be evaluated to some value.
You can shorten the way you write statements that do a math operation and assign the result to a variable. For example a = a + 5
can be shorted to a += 5
. Here are more examples:
xxxxxxxxxx
var num = 4;
num += 2;
console.log(num);
num -= 3;
console.log(num);
num *= 10;
console.log(num);
num /= 5;
console.log(num);
num %= 2;
console.log(num);
Run this code in the console to see what the limits are for numeric expressions in JavaScript.
xxxxxxxxxx
var biggestNum = Number.MAX_VALUE;
var smallestNum = Number.MIN_VALUE;
var infiniteNum = Number.POSITIVE_INFINITY;
var negInfiniteNum = Number.NEGATIVE_INFINITY;
var biggestInt = 9007199254740991;
var smallestInt = -9007199254740991;
Test an expression to see if it is finite: isFinite(n)
Try this with 1/0
in place of n
.
Test an expression to see if it is a valid number: isNaN(n)
Beginning JavaScript Lecture Notes by Brian Bird, written 2018, revised , are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
xxxxxxxxxx