Skip to content

Commit 120b66d

Browse files
Markus Amalthea Magnusonajpiano
Markus Amalthea Magnuson
authored andcommitted
Various small fixes on the JavaScript 101 Conditional Code page. Fixes jquery#350.
1 parent 5537f11 commit 120b66d

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

page/javascript-101/conditional-code.md

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,30 @@ source: http://jqfundamentals.com/legacy
55
attribution:
66
- jQuery Fundamentals
77
---
8+
89
Sometimes a block of code should only be run under certain conditions. Flow control – via `if` and `else` blocks – lets you run code if certain conditions have been met.
910

1011
```
1112
// Flow control
13+
1214
var foo = true;
1315
var bar = false;
1416
1517
if ( bar ) {
16-
// this code will never run
18+
// This code will never run.
1719
console.log( "hello!" );
1820
}
1921
2022
if ( bar ) {
2123
22-
// this code won't run
24+
// This code won't run.
2325
2426
} else {
2527
2628
if ( foo ) {
27-
28-
// this code will run
29-
29+
// This code will run.
3030
} else {
31-
32-
// this code would run if foo and bar were both false
33-
31+
// This code would run if foo and bar were both false.
3432
}
3533
3634
}
@@ -45,31 +43,31 @@ Be mindful not to define functions with the same name multiple times within sepa
4543
In order to use flow control successfully, it's important to understand which kinds of values are "truthy" and which kinds of values are "falsy." Sometimes, values that seem like they should evaluate one way actually evaluate another.
4644

4745
```
48-
// Values that evaluate to true
46+
// Values that evaluate to true:
4947
"0";
5048
"any string";
51-
[]; // an empty array
52-
{}; // an empty object
53-
1; // any non-zero number
49+
[]; // An empty array.
50+
{}; // An empty object.
51+
1; // Any non-zero number.
5452
```
5553

5654
```
57-
// Values that evaluate to false
58-
""; // an empty string
59-
NaN; // JavaScript's "not-a-number" variable
55+
// Values that evaluate to false:
56+
""; // An empty string.
57+
NaN; // JavaScript's "not-a-number" variable.
6058
null;
61-
undefined; // be careful -- undefined can be redefined!
62-
0; // the number zero
59+
undefined; // Be careful -- undefined can be redefined!
60+
0; // The number zero.
6361
```
6462

6563
## Conditional Variable Assignment with the Ternary Operator
6664

6765
Sometimes a variable should be set depending on some condition. An `if`/`else` statement works, but in many cases the ternary operator is more convenient. The ternary operator tests a condition; if the condition is true, it returns a certain value, otherwise it returns a different value.
6866

6967
The ternary operator:
68+
7069
```
71-
// set foo to 1 if bar is true;
72-
// otherwise, set foo to 0
70+
// Set foo to 1 if bar is true; otherwise, set foo to 0:
7371
var foo = bar ? 1 : 0;
7472
```
7573

@@ -81,6 +79,7 @@ Rather than using a series of `if`/`else` blocks, sometimes it can be useful to
8179

8280
```
8381
// A switch statement
82+
8483
switch ( foo ) {
8584
8685
case "bar":
@@ -122,7 +121,7 @@ if ( stuffToDo[ foo ] ) {
122121
123122
} else {
124123
125-
stuffToDo["default"]();
124+
stuffToDo[ "default" ]();
126125
127126
}
128127
```

0 commit comments

Comments
 (0)