Dr. Greg Bernstein
January 31st, 2021
Just like C++ and Java. Used to group statements:
{
statement_1;
statement_2;
statement_n;
}
Let’s learn about variable scoping with raw blocks.
Just like C++ and Java:
if (condition) {
statement_1;
} else {
statement_2;
}
You’ll sometimes see very terse JavaScript conditional statements. These take advantage of the following evaluating to false
in an if
:
false
, undefined
, null
, 0
, NaN
, and the empty string (""
)
Similar to C++ and Java, however the labels are usually strings. Don’t forget the break
statement!
switch (expression) {
case label_1:
statements_1
[break;]
case label_2:
statements_2
[break;]
...
default:
statements_def
[break;]
}
JavaScript supports exceptions similar to C++, Java, and Python.
throw
statement you can use to throw any object including numbers and stringstry
/catch
block when you want to handle exceptions.Examples from MDN:
throw 'Error2'; // String type
throw 42; // Number type
throw true; // Boolean type
throw {toString: function() { return "I'm an object!"; } };
Basic structure:
try {
// A bunch of statements
// that could throw an exception
}
catch(e) {
// Statements to handle the exception
// The exception is gotten via the "e"
// parameter. You can give this parameter any name.
}
General form
for ([initialExpression]; [condition]; [incrementExpression])
statement
Example:
for(var i = 0; i < 10; i++) {
console.log(i.toString() + " cubed = " + i*i*i);
}
General form:
do
statement
while (condition);
Example:
var i = 3;
do {
console.log(i++);
} while(i < 5);
General form
while(condition)
statement
Example:
message = "Code! ";
while (message.length < 40) {
message += "The Web, ";
}
console.log(message);
From MDN
Use the break statement to terminate a loop or switch statement.
continue
StatementFrom MDN
The continue statement can be used to restart a while, do-while or for statement. It terminates the current iteration of the innermost enclosing while, do-while, or for statement and continues execution of the loop with the next iteration. In a while loop, it jumps back to the condition. In a for loop, it jumps to the increment-expression.
For enumerating properties of objects:
for (variable in object) {
statements
}
Example:
myObject = {name: "Dr. B", topic: "Web Dev", fun: true};
for (var prop in myObject) {
console.log("Property: " + prop + ", has value: " + myObject[prop]);
}
Caution objects other than those you directly create may inherit many other properties. Use the method .hasOwnProperty(propString)
to test.
A nicer way to loop over iterable objects such as arrays.
for (variable of object) {
statement
}
Example:
myArray = ["Let's", "program", "the", "web"];
for (var word of myArray) {
console.log(word);
}
From MDN
var arr = new Array(element0, element1, ..., elementN);
var arr = Array(element0, element1, ..., elementN);
var arr = [element0, element1, ..., elementN];
Visit MDN Array frequently and often.
DO NOT REINVENT THE WHEEL!!!
There are array methods to help you perform a huge range of tasks!
Searching: find()
, findIndex()
, includes()
, indexOf()
, lastIndexOf(searchElement[, fromIndex])
Combining and string conversion: concat()
, join(deliminator = ',')
Stack, List operations: push()
, pop()
, shift()
, unshift()
Modification: slice(start_index, upto_index)
, splice(index, count_to_remove, addElement1, addElement2, ...)
, reverse()
sort()
: sorts the elements of an array. Can take a callback function for custom sorting.
forEach(callback[, thisObject])
, map(callback[, thisObject])
, filter(callback[, thisObject])
every(callback[, thisObject])
, some(callback[, thisObject])
reduce(callback[, initialValue])
, reduceRight(callback[, initialValue])
Check your understanding