Skip to content

Commit 02acf72

Browse files
committed
converted 'conditional-code' to the new convention
1 parent 3b2a242 commit 02acf72

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

page/javascript-101/conditional-code.md

+18-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ attribution: jQuery Fundamentals
44
---
55
Sometimes you only want to run a block of code under certain conditions. Flow control — via if and else blocks — lets you run code only under certain conditions.
66

7-
<javascript caption="Flow control">
7+
``` js
8+
// Flow control
89
var foo = true;
910
var bar = false;
1011

@@ -22,9 +23,9 @@ if (bar) {
2223
// this code would run if foo and bar were both false
2324
}
2425
}
25-
</javascript>
26+
```
2627

27-
<div class="note" markdown="1">
28+
<div class="note">
2829
While curly braces aren't strictly required around single-line if statements,
2930
using them consistently, even when they aren't strictly required, makes for
3031
vastly more readable code.
@@ -39,20 +40,22 @@ In order to use flow control successfully, it's important to understand which
3940
kinds of values are "truthy" and which kinds of values are "falsy." Sometimes,
4041
values that seem like they should evaluate one way actually evaluate another.
4142

42-
<javascript caption="Values that evaluate to true">
43+
``` js
44+
// Values that evaluate to true
4345
'0';
4446
'any string';
4547
[]; // an empty array
4648
{}; // an empty object
4749
1; // any non-zero number
48-
</javascript>
50+
```
4951

50-
<javascript caption="Values that evaluate to false">
52+
``` js
53+
// Values that evaluate to false
5154
''; // an empty string
5255
NaN; // JavaScript's "not-a-number" variable
5356
null;
5457
undefined; // be careful -- undefined can be redefined!
55-
</javascript>
58+
```
5659

5760
## Conditional Variable Assignment with The Ternary Operator
5861

@@ -62,11 +65,12 @@ more convenient. The ternary operator tests a condition; if the
6265
condition is true, it returns a certain value, otherwise it returns a different
6366
value.
6467

65-
<javascript caption="The ternary operator">
68+
``` js
69+
The ternary operator
6670
// set foo to 1 if bar is true;
6771
// otherwise, set foo to 0
6872
var foo = bar ? 1 : 0;
69-
</javascript>
73+
```
7074

7175
While the ternary operator can be used without assigning the return value to a
7276
variable, this is generally discouraged.
@@ -78,7 +82,8 @@ useful to use a switch statement instead. Switch statements look
7882
at the value of a variable or expression, and run different blocks of code
7983
depending on the value.
8084

81-
<javascript caption="A switch statement">
85+
``` js
86+
// A switch statement
8287
switch (foo) {
8388

8489
case 'bar':
@@ -94,13 +99,13 @@ switch (foo) {
9499
break;
95100

96101
}
97-
</javascript>
102+
```
98103

99104
Switch statements have somewhat fallen out of favor in JavaScript, because
100105
often the same behavior can be accomplished by creating an object that has more
101106
potential for reuse, testing, etc. For example:
102107

103-
<javascript>
108+
``` js
104109
var stuffToDo = {
105110
'bar' : function() {
106111
alert('the value was bar -- yay!');
@@ -120,6 +125,6 @@ if (stuffToDo[foo]) {
120125
} else {
121126
stuffToDo['default']();
122127
}
123-
</javascript>
128+
```
124129

125130
We'll look at objects in greater depth later in this chapter.

0 commit comments

Comments
 (0)