Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Correct code tabbing in conditional-code.md #33

Merged
merged 1 commit into from

2 participants

James Padolsey Addy Osmani
James Padolsey

Not sure if you're accepting pull requests yet. Tiny fix. Some odd tabbing if you go here: http://stage.learn.jquery.com/javascript-101/conditional-code/

Addy Osmani
Collaborator

We are indeed. Thanks James!

Addy Osmani addyosmani merged commit c318cbe into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 30, 2011
  1. James Padolsey

    Correct code tabbing

    padolsey authored
This page is out of date. Refresh to see the latest.
Showing with 54 additions and 55 deletions.
  1. +54 −55 content/javascript-101/conditional-code.md
109 content/javascript-101/conditional-code.md
View
@@ -7,23 +7,23 @@ 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.
<javascript caption="Flow control">
- var foo = true;
- var bar = false;
-
- if (bar) {
- // this code will never run
- console.log('hello!');
- }
-
- if (bar) {
- // this code won't run
+var foo = true;
+var bar = false;
+
+if (bar) {
+ // this code will never run
+ console.log('hello!');
+}
+
+if (bar) {
+ // this code won't run
+} else {
+ if (foo) {
+ // this code will run
} else {
- if (foo) {
- // this code will run
- } else {
- // this code would run if foo and bar were both false
- }
+ // this code would run if foo and bar were both false
}
+}
</javascript>
<div class="note" markdown="1">
@@ -42,18 +42,18 @@ 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.
<javascript caption="Values that evaluate to true">
- '0';
- 'any string';
- []; // an empty array
- {}; // an empty object
- 1; // any non-zero number
+'0';
+'any string';
+[]; // an empty array
+{}; // an empty object
+1; // any non-zero number
</javascript>
<javascript caption="Values that evaluate to false">
- ''; // an empty string
- NaN; // JavaScript's "not-a-number" variable
- null;
- undefined; // be careful -- undefined can be redefined!
+''; // an empty string
+NaN; // JavaScript's "not-a-number" variable
+null;
+undefined; // be careful -- undefined can be redefined!
</javascript>
## Conditional Variable Assignment with The Ternary Operator
@@ -65,9 +65,9 @@ condition is true, it returns a certain value, otherwise it returns a different
value.
<javascript caption="The ternary operator">
- // set foo to 1 if bar is true;
- // otherwise, set foo to 0
- var foo = bar ? 1 : 0;
+// set foo to 1 if bar is true;
+// otherwise, set foo to 0
+var foo = bar ? 1 : 0;
</javascript>
While the ternary operator can be used without assigning the return value to a
@@ -81,21 +81,21 @@ at the value of a variable or expression, and run different blocks of code
depending on the value.
<javascript caption="A switch statement">
- switch (foo) {
+switch (foo) {
- case 'bar':
- alert('the value was bar -- yay!');
- break;
+ case 'bar':
+ alert('the value was bar -- yay!');
+ break;
- case 'baz':
- alert('boo baz :(');
- break;
+ case 'baz':
+ alert('boo baz :(');
+ break;
- default:
- alert('everything else is just ok');
- break;
+ default:
+ alert('everything else is just ok');
+ break;
- }
+}
</javascript>
Switch statements have somewhat fallen out of favor in JavaScript, because
@@ -103,26 +103,25 @@ often the same behavior can be accomplished by creating an object that has more
potential for reuse, testing, etc. For example:
<javascript>
+var stuffToDo = {
+ 'bar' : function() {
+ alert('the value was bar -- yay!');
+ },
- var stuffToDo = {
- 'bar' : function() {
- alert('the value was bar -- yay!');
- },
+ 'baz' : function() {
+ alert('boo baz :(');
+ },
- 'baz' : function() {
- alert('boo baz :(');
- },
-
- 'default' : function() {
- alert('everything else is just ok');
- }
- };
-
- if (stuffToDo[foo]) {
- stuffToDo[foo]();
- } else {
- stuffToDo['default']();
+ 'default' : function() {
+ alert('everything else is just ok');
}
+};
+
+if (stuffToDo[foo]) {
+ stuffToDo[foo]();
+} else {
+ stuffToDo['default']();
+}
</javascript>
We'll look at objects in greater depth later in this chapter.
Something went wrong with that request. Please try again.