Javascript
/
Fundamentals
- 1 Fundamentals 11
-
Hello World S
-
Code structure S
-
Use strict S
-
Variables S
-
Data types S
-
Type conversions S
-
Maths S
-
Comparitions S
-
Conditional S
-
Loops S
-
Functions S
- 2 Testing 2
-
Mocha S
-
Nested describe S
- 3 Objects 4
-
Basics S
-
Reference S
-
Methods S
-
Constructor S
- 4 Types 5
-
Primitives S
-
Numbers S
-
Strings S
-
Arrays S
-
Json S
- 5 Classes 3
-
Constructor S
-
Binding S
-
Inheritance S
S
R
Q
For
Loops are a way to repeat the same code multiple times.
/**
* For looop
*
* A single execution of the loop body is called an iteration
* The counter variable i is declared in the loop
*/
for (let i=0; i<3; i++) {
console.log(i); // 0 1 2
}
Labels
Sometimes we need to break out from multiple nested loops.
/**
* For label
*
* A label is an identifier with a colon before the loop
* Labels are not to jump into an arbitrary place in the code
*/
outer: while(true) {
inner: for(let i=0; i<10; i++) {
if (i == 5) {
break outer;
}
console.log(i); // 1 1 2 3 4
}
}
Switch
The value must be of the same type to match.
/**
* Switch statement
*
* The value must be of the same type to match
* The ability to group cases is a side-effect of how switch works
*/
let a = 2;
switch(a) {
case 2: // group of two cases (no break)
case '2':
console.log("2 or '2'");
break;
default:
console.log('no value'); // 2 or '2'
}
➥ Questions