Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
1afacaa
style conformance for about-jquery/how-jquery-works
Nov 7, 2012
a98b0bf
style conformance for javascript-101/getting-started
Nov 7, 2012
776c4aa
style conformance for javascript-101/syntax-basics
Nov 7, 2012
f7641f4
style conformance for javascript-101/running-code
Nov 7, 2012
87ce1f2
style conformance for javascript-101/types
Nov 8, 2012
74be2db
style conformance for javascript-101/operators
Nov 8, 2012
cc01ee8
style conformance for javascript-101/conditional-code
Nov 8, 2012
2b4cf16
found the section on truthy/falsy
Nov 8, 2012
70b2003
style conformance for javascript-101/loops
Nov 8, 2012
6b04bb6
style conformance for javascript-101/arrays
Nov 8, 2012
0d7b57e
adding links to objects/arrays sections
Nov 8, 2012
f97c941
style conformance for javascript-101/objects
Nov 8, 2012
47fda0e
style conformance for javascript-101/functions
Nov 8, 2012
e498797
style conformance for javascript-101/testing-type
Nov 8, 2012
f1c8aff
link to testing-type section
Nov 8, 2012
2254121
style conformance for javascript-101/this-keyword
Nov 8, 2012
2dfb23a
style conformance for javascript-101/scope
Nov 8, 2012
1cb18ad
style conformance for javascript-101/closures
Nov 8, 2012
4259929
horizontal scrolling issues
Nov 8, 2012
a709de1
consistency with —
Nov 8, 2012
adcbaeb
style conformance for using-jquery-core/dollar-object-vs-function
Nov 8, 2012
bbe819e
style conformance for using-jquery-core/document-ready
Nov 8, 2012
bc6b21c
style conformance for using-jquery-core/avoid-conflicts-other-libraries
Nov 8, 2012
24b91ce
style conformance for using-jquery-core/attributes
Nov 8, 2012
8989211
style conformance for using-jquery-core/selecting-elements
Nov 8, 2012
d8c1ff5
style conformance for using-jquery-core/working-with-selections
Nov 9, 2012
3f9e54a
style conformance for using-jquery-core/manipulating-elements
Nov 9, 2012
947dff6
style conformance for using-jquery-core/data-methods
Nov 9, 2012
b17307b
style conformance for using-jquery-core/utility-methods
Nov 9, 2012
40c11cf
style conformance for using-jquery-core/jquery-object
Nov 9, 2012
96f06d4
style conformance for using-jquery-core/traversing
Nov 9, 2012
a52fe58
style conformance for using-jquery-core/css-styling-dimensions
Nov 9, 2012
6256c9e
style conformance for using-jquery-core/iterating
Nov 9, 2012
8f96503
style conformance for using-jquery-core/understanding-index
Nov 9, 2012
e512536
deleting obsolete exercises.md page
Nov 9, 2012
d6d5dcb
Update config-sample to reflect local.* instead of dev.*
ajpiano Nov 9, 2012
4ae27eb
Upgrade to grunt-jquery-content 0.5.10, add grunt-check-modules to gr…
ajpiano Nov 9, 2012
495a7b0
README.md updates
ajpiano Nov 9, 2012
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
style conformance for javascript-101/conditional-code
  • Loading branch information
jorydotcom committed Nov 8, 2012
commit cc01ee863ca2825ddffffde79376ae231946ec06
39 changes: 11 additions & 28 deletions page/javascript-101/conditional-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ source: http://jqfundamentals.com/legacy
attribution:
- jQuery Fundamentals
---
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.
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.

```
// Flow control
Expand Down Expand Up @@ -36,20 +36,13 @@ if ( bar ) {
}
```

<div class="note">
While curly braces aren't strictly required around single-line if statements,
using them consistently, even when they aren't strictly required, makes for
vastly more readable code.
While curly braces aren't strictly required around single-line `if` statements, using them consistently, even when they aren't strictly required, makes for vastly more readable code.

Be mindful not to define functions with the same name multiple times within
separate if/else blocks, as doing so may not have the expected result.
</div>
Be mindful not to define functions with the same name multiple times within separate if/else blocks, as doing so may not have the expected result.

## Truthy and Falsy Things

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.
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.

```
// Values that evaluate to true
Expand All @@ -68,30 +61,22 @@ null;
undefined; // be careful -- undefined can be redefined!
```

## Conditional Variable Assignment with The Ternary Operator
## Conditional Variable Assignment with the Ternary Operator

Sometimes you want to set a variable to a value depending on some condition.
You could use an if/else statement, 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.
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.

The ternary operator
The ternary operator:
```
// set foo to 1 if bar is true;
// otherwise, set foo to 0
var foo = bar ? 1 : 0;
```

While the ternary operator can be used without assigning the return value to a
variable, this is generally discouraged.
While the ternary operator can be used without assigning the return value to a variable, this is generally discouraged.

## Switch Statements

Rather than using a series of if/else if/else blocks, sometimes it can be
useful to use a switch statement instead. Switch statements look
at the value of a variable or expression, and run different blocks of code
depending on the value.
Rather than using a series of `if`/`else` blocks, sometimes it can be useful to use a `switch` statement instead. `Switch` statements look at the value of a variable or expression, and run different blocks of code depending on the value.

```
// A switch statement
Expand All @@ -116,9 +101,7 @@ switch ( foo ) {
}
```

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

```
var stuffToDo = {
Expand Down Expand Up @@ -154,4 +137,4 @@ if ( stuffToDo[foo] ) {
}
```

We'll look at objects in greater depth later in this chapter.
Objects are covered further in the [Types](/types) and [Objects](/objects) sections.