|
| 1 | +--- |
| 2 | +chapter : "js101" |
| 3 | +section: "javascript-basics" |
| 4 | +title: "Operators" |
| 5 | +--- |
| 6 | +## Basic Operators |
| 7 | + |
| 8 | +## Basic Operators |
| 9 | + |
| 10 | +Basic operators allow you to manipulate values. |
| 11 | + |
| 12 | +<div class="example" markdown="1"> |
| 13 | +Concatenation |
| 14 | + |
| 15 | + var foo = 'hello'; |
| 16 | + var bar = 'world'; |
| 17 | + console.log(foo + ' ' + bar); // 'hello world' |
| 18 | +</div> |
| 19 | + |
| 20 | +<div class="example" markdown="1"> |
| 21 | +Multiplication and division |
| 22 | + |
| 23 | + 2 * 3; |
| 24 | + 2 / 3; |
| 25 | +</div> |
| 26 | + |
| 27 | +<div class="example" markdown="1"> |
| 28 | +Incrementing and decrementing |
| 29 | + |
| 30 | + var i = 1; |
| 31 | + var j = ++i; // pre-increment: j equals 2; i equals 2 |
| 32 | + var k = i++; // post-increment: k equals 2; i equals 3 |
| 33 | +</div> |
| 34 | + |
| 35 | +### Operations on Numbers & Strings |
| 36 | + |
| 37 | +In JavaScript, numbers and strings will occasionally behave in ways you might not expect. |
| 38 | + |
| 39 | +<div class="example" markdown="1"> |
| 40 | +Addition vs. concatenation |
| 41 | + |
| 42 | + var foo = 1; |
| 43 | + var bar = '2'; |
| 44 | + console.log(foo + bar); // 12. uh oh |
| 45 | +</div> |
| 46 | + |
| 47 | +<div class="example" markdown="1"> |
| 48 | +Forcing a string to act as a number |
| 49 | + |
| 50 | + var foo = 1; |
| 51 | + var bar = '2'; |
| 52 | + |
| 53 | + // coerce the string to a number |
| 54 | + console.log(foo + Number(bar)); |
| 55 | +</div> |
| 56 | + |
| 57 | + |
| 58 | +The Number constructor, when called as a function (like above) will have the |
| 59 | +effect of casting its argument into a number. You could also use the unary plus |
| 60 | +operator, which does the same thing: |
| 61 | + |
| 62 | +<div class="example" markdown="1"> |
| 63 | +Forcing a string to act as a number (using the unary-plus operator) |
| 64 | +console.log(foo + +bar); |
| 65 | +</div> |
| 66 | + |
| 67 | +### Logical Operators |
| 68 | + |
| 69 | +Logical operators allow you to evaluate a series of operands using AND and OR operations. |
| 70 | + |
| 71 | +<div class="example" markdown="1"> |
| 72 | +Logical AND and OR operators |
| 73 | + |
| 74 | + var foo = 1; |
| 75 | + var bar = 0; |
| 76 | + var baz = 2; |
| 77 | + |
| 78 | + foo || bar; // returns 1, which is true |
| 79 | + bar || foo; // returns 1, which is true |
| 80 | + |
| 81 | + foo && bar; // returns 0, which is false |
| 82 | + foo && baz; // returns 2, which is true |
| 83 | + baz && foo; // returns 1, which is true |
| 84 | +</div> |
| 85 | + |
| 86 | +Though it may not be clear from the example, the || operator returns the value |
| 87 | +of the first truthy operand, or, in cases where neither operand is truthy, |
| 88 | +it'll return the last of both operands. The && operator returns the value of |
| 89 | +the first false operand, or the value of the last operand if both operands are |
| 90 | +truthy. |
| 91 | + |
| 92 | +Be sure to consult the section called “Truthy and Falsy Things” for more |
| 93 | +details on which values evaluate to true and which evaluate to false. |
| 94 | + |
| 95 | +<div class="note"> |
| 96 | +## Note |
| 97 | + |
| 98 | +You'll sometimes see developers use these logical operators for flow control |
| 99 | +instead of using if statements. For example: |
| 100 | + |
| 101 | +<div class="example" markdown="1"> |
| 102 | +// do something with foo if foo is truthy |
| 103 | +foo && doSomething(foo); |
| 104 | + |
| 105 | +// set bar to baz if baz is truthy; |
| 106 | +// otherwise, set it to the return |
| 107 | +// value of createBar() |
| 108 | +var bar = baz || createBar(); |
| 109 | +</div> |
| 110 | + |
| 111 | +This style is quite elegant and pleasantly terse; that said, it can be really |
| 112 | +hard to read, especially for beginners. I bring it up here so you'll recognize |
| 113 | +it in code you read, but I don't recommend using it until you're extremely |
| 114 | +comfortable with what it means and how you can expect it to behave. |
| 115 | + |
| 116 | +### Comparison Operators |
| 117 | + |
| 118 | +Comparison operators allow you to test whether values are equivalent or whether |
| 119 | +values are identical. |
| 120 | + |
| 121 | +<div class="example" markdown="1"> |
| 122 | +Comparison operators |
| 123 | + |
| 124 | + var foo = 1; |
| 125 | + var bar = 0; |
| 126 | + var baz = '1'; |
| 127 | + var bim = 2; |
| 128 | + |
| 129 | + foo == bar; // returns false |
| 130 | + foo != bar; // returns true |
| 131 | + foo == baz; // returns true; careful! |
| 132 | + |
| 133 | + foo === baz; // returns false |
| 134 | + foo !== baz; // returns true |
| 135 | + foo === parseInt(baz); // returns true |
| 136 | + |
| 137 | + foo > bim; // returns false |
| 138 | + bim > baz; // returns true |
| 139 | + foo <= baz; // returns true |
| 140 | +</div> |
0 commit comments