Skip to content

Various small fixes on the JavaScript 101 Operators page. #349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
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
24 changes: 14 additions & 10 deletions page/javascript-101/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ source: http://jqfundamentals.com/legacy
attribution:
- jQuery Fundamentals
---

Basic operators allow you to manipulate values.

```
Expand All @@ -16,13 +17,13 @@ console.log( foo + " " + bar ); // "hello world"
```

```
// Multiplication and division
// Multiplication and division.
2 * 3;
2 / 3;
```

```
// Incrementing and decrementing
// Incrementing and decrementing.
// The pre-increment operator increments the operand before any further processing.
var i = 1;
console.log( ++i ); // 2 - because i was incremented before evaluation
Expand All @@ -47,26 +48,27 @@ console.log( foo + bar ); // 12
```

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

console.log( foo + Number(bar) ); // 3
```

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:
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 ); // 3
```

## Logical Operators

Logical operators allow evaluation of 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
// Logical AND and OR operators.

var foo = 1;
var bar = 0;
var baz = 2;
Expand All @@ -92,10 +94,10 @@ In the above example, the `||` operator returns the value of the first truthy op
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
// Do something with foo if foo is truthy.
foo && doSomething( foo );

// set bar to baz if baz is truthy;
// Set bar to baz if baz is truthy;
// otherwise, set it to the return value of createBar()
var bar = baz || createBar();
```
Expand All @@ -107,7 +109,8 @@ This style is quite elegant and pleasantly terse; that said, it can be really ha
Comparison operators allow you to test whether values are equivalent or whether values are identical.

```
// Comparison operators
// Comparison operators.

var foo = 1;
var bar = 0;
var baz = "1";
Expand All @@ -125,4 +128,5 @@ foo > bim; // false
bim > baz; // true
foo <= baz; // 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").