Chapter 5: Statements Flashcards
(23 cards)
The let and function are _______ statements—they declare or define variables and
functions
declaration
function syntax
function funcname([arg1 [, arg2 […, argn]]]) {
statements
}
Eaxmple of function
var f = function(x) { return x+1; } // Expression assigned to a variable
if statement syntax
if (expression)
statement
switch statement syntax
switch(n) {
case 1: // Start here if n == 1
// Execute code block #1.
break;
// Stop here
case 2: // Start here if n == 2
// Execute code block #2.
break; // Stop here
case 3: // Start here if n == 3
// Execute code block #3.
break; // Stop here
default: // If all else fails…
// Execute code block #4.
break; // stop here
}
Example while loop
let count = 0;
while(count < 10) {
console.log(count);
count++;
}
do while loop
same as java
For loop
same as java
The for/of loop works with _____ objects
iterable
Examplefor of loop
let data = [1, 2, 3, 4, 5, 6, 7, 8, 9], sum = 0;
for(let element of data) {
sum += element;
}
sum
If you want to iterate through the properties of an object, you can use
the for/in loop (introduced in §5.4.5), or use for/of with the
_________ method
Object.keys()
________ returns an array of arrays, where each inner
array represents a key/value pair for one property of the object
Object.entries()
Are string iterable in javascript
yes
A for/in loop looks a lot like a for/of loop, with the of keyword
changed to in. While a for/of loop requires an ______ object after
the of, a for/in loop works with ____ object after the in
iterable, any
Example of for in
for(let p in o) { // Assign property names of o to
variable p
console.log(o[p]); // Print the value of each property
}
When
working with arrays, you almost always want to use for/of instead
of for/in.
True
Jump statements
continue, break, return
The yield statement (python) is much like the return statement but is used
only in ES6 generator functions (see §12.3) to produce the next value
in the generated sequence of values without actually ______
returning
The _____ statement runs a block of code as if the properties of a
specified object were variables in scope for that code
with
with syntax
with (object)
statement
JavaScript there is really no reason to use var instead of let.
True
Declare and initialize one or more constants
const
Declare names for values defined in other modules
import