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/operators
  • Loading branch information
jorydotcom committed Nov 8, 2012
commit 74be2dbd9ef1759f0db7bdaa23802710aea3a6e4
65 changes: 25 additions & 40 deletions page/javascript-101/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,57 +24,53 @@ console.log( foo + " " + bar ); // "hello world"
```
// Incrementing and decrementing
// The pre-increment operator increments the operand before any further processing.
// pre-increment
// pre-increment:
var i = 1;

console.log( ++i ); // => 2

console.log( i ); // => 2

// The post-increment operator increments the operand after processing it.
// post-increment
// post-increment:
var i = 1;

console.log( i++ ); // => 1. This is because i was processed first
console.log( i++ ); // => 1. This is because i was processed first.

console.log( i ); // => 2. This is because the operand was incremented after processing in the previous step.
console.log( i ); // => 2. This is because the operand was incremented after processing
// in the previous step.
```

## Operations on Numbers & Strings

In JavaScript, numbers and strings will occasionally behave in ways you might
not expect.
In JavaScript, numbers and strings will occasionally behave in unexpected ways.

```
// Addition vs. Concatenation
var foo = 1;
var bar = "2";

console.log( foo + bar ); // 12. uh oh
console.log( foo + bar ); // => 12
```

```
// Forcing a string to act as a number
// Coercing a string to act as a number:
var foo = 1;
var bar = "2";

// coerce the string to a number
console.log( foo + Number(bar) );
```

The Number constructor, when called as a function (like above) will have the
effect of casting its argument into a number. You could also use the unary plus
operator, which does the same thing:
The Number constructor, when called as a function (as in the above example), will have the effect of casting its argument into a number. The unary plus operator also does the same thing:

```
// Forcing a string to act as a number (using the unary-plus operator)
// Forcing a string to act as a number (using the unary plus operator):
console.log( foo + +bar );
```

## Logical Operators

Logical operators allow you to evaluate a series of operands using AND and OR
operations.
Logical operators allow evaluation of a series of operands using AND ( `&&` ) and OR ( `||` ) operations.

```
// Logical AND and OR operators
Expand All @@ -90,37 +86,25 @@ foo && baz; // returns 2, which is true
baz && foo; // returns 1, which is true
```

Though it may not be clear from the example, the `||` operator returns the value
of the first truthy operand, or, in cases where neither operand is truthy,
it'll return the last operand. The `&&` operator returns the value of
the first false operand, or the value of the last operand if both operands are
truthy.
In the above example, the `||` operator returns the value of the first truthy operand, or in cases where neither operand is truthy, it returns the last operand. The `&&` operator returns the value of the first false operand, or the value of the last operand if both operands are truthy.

Be sure to see the section called “Truthy and Falsy Things” for more
details on which values evaluate to true and which evaluate to false.
You'll sometimes see developers use these logical operators for flow control instead of using `if` statements. For example:

<div class="note">
You'll sometimes see developers use these logical operators for flow control
instead of using if statements. For example:

// do something with foo if foo is truthy
foo && doSomething( foo );
```
// do something with foo if foo is truthy
foo && doSomething( foo );

// set bar to baz if baz is truthy;
// otherwise, set it to the return
// value of createBar()
var bar = baz || createBar();
// set bar to baz if baz is truthy;
// otherwise, set it to the return
// value of createBar()
var bar = baz || createBar();
```

This style is quite elegant and pleasantly terse; that said, it can be really
hard to read, especially for beginners. I bring it up here so you'll recognize
it in code you read, but I don't recommend using it until you're extremely
comfortable with what it means and how you can expect it to behave.
</div>
This style is quite elegant and pleasantly terse; that said, it can be really hard to read or use, especially for beginners.

## Comparison Operators

Comparison operators allow you to test whether values are equivalent or whether
values are identical.
Comparison operators allow you to test whether values are equivalent or whether values are identical.

```
// Comparison operators
Expand All @@ -131,7 +115,7 @@ var bim = 2;

foo == bar; // returns false
foo != bar; // returns true
foo == baz; // returns true; careful!
foo == baz; // returns true; but note that the types are different

foo === baz; // returns false
foo !== baz; // returns true
Expand All @@ -141,3 +125,4 @@ foo > bim; // returns false
bim > baz; // returns true
foo <= baz; // returns true
```
For more information about comparison operators, visit the [Mozilla Developer Network](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Comparison_Operators "MDN - Comparison Operators").