@@ -4,19 +4,22 @@ attribution: jQuery Fundamentals
4
4
---
5
5
Basic operators allow you to manipulate values.
6
6
7
- <javascript caption =" Concatenation " >
7
+ ``` js
8
+ // Concatenation
8
9
var foo = ' hello' ;
9
10
var bar = ' world' ;
10
11
console .log (foo + ' ' + bar); // 'hello world'
11
- </ javascript >
12
+ ```
12
13
13
- <javascript caption =" Multiplication and division " >
14
+ ``` js
15
+ // Multiplication and division
14
16
2 * 3 ;
15
17
2 / 3 ;
16
- </ javascript >
18
+ ```
17
19
18
- <javascript caption =" Incrementing and decrementing " >
19
- The pre-increment operator increments the operand before any further processing.
20
+ ``` js
21
+ // Incrementing and decrementing
22
+ // The pre-increment operator increments the operand before any further processing.
20
23
// pre-increment
21
24
var i = 1 ;
22
25
console .log (++ i); // => 2
@@ -27,41 +30,45 @@ The post-increment operator increments the operand after processing it.
27
30
var i = 1 ;
28
31
console .log (i++ ); // => 1. This is because i was processed first
29
32
console .log (i); // => 2. This is because the operand was incremented after processing in the previous step.
30
- </ javascript >
33
+ ```
31
34
32
35
## Operations on Numbers & Strings
33
36
34
37
In JavaScript, numbers and strings will occasionally behave in ways you might
35
38
not expect.
36
39
37
- <javascript caption =" Addition vs. Concatenation " >
40
+ ``` js
41
+ // Addition vs. Concatenation
38
42
var foo = 1 ;
39
43
var bar = ' 2' ;
40
44
console .log (foo + bar); // 12. uh oh
41
- </ javascript >
45
+ ```
42
46
43
- <javascript caption =" Forcing a string to act as a number " >
47
+ ``` js
48
+ // Forcing a string to act as a number
44
49
var foo = 1 ;
45
50
var bar = ' 2' ;
46
51
47
52
// coerce the string to a number
48
53
console .log (foo + Number (bar));
49
- </ javascript >
54
+ ```
50
55
51
56
The Number constructor, when called as a function (like above) will have the
52
57
effect of casting its argument into a number. You could also use the unary plus
53
58
operator, which does the same thing:
54
59
55
- <javascript caption =" Forcing a string to act as a number (using the unary-plus operator) " >
60
+ ``` js
61
+ // Forcing a string to act as a number (using the unary-plus operator)
56
62
console .log (foo + + bar);
57
- </ javascript >
63
+ ```
58
64
59
65
## Logical Operators
60
66
61
67
Logical operators allow you to evaluate a series of operands using AND and OR
62
68
operations.
63
69
64
- <javascript caption =" Logical AND and OR operators " >
70
+ ``` js
71
+ // Logical AND and OR operators
65
72
var foo = 1 ;
66
73
var bar = 0 ;
67
74
var baz = 2 ;
@@ -72,7 +79,7 @@ bar || foo; // returns 1, which is true
72
79
foo && bar; // returns 0, which is false
73
80
foo && baz; // returns 2, which is true
74
81
baz && foo; // returns 1, which is true
75
- </ javascript >
82
+ ```
76
83
77
84
Though it may not be clear from the example, the ` || ` operator returns the value
78
85
of the first truthy operand, or, in cases where neither operand is truthy,
@@ -83,7 +90,7 @@ truthy.
83
90
Be sure to see the section called “Truthy and Falsy Things” for more
84
91
details on which values evaluate to true and which evaluate to false.
85
92
86
- <div class =" note " markdown = " 1 " >
93
+ <div class =" note " >
87
94
You'll sometimes see developers use these logical operators for flow control
88
95
instead of using if statements. For example:
89
96
@@ -106,7 +113,8 @@ comfortable with what it means and how you can expect it to behave.
106
113
Comparison operators allow you to test whether values are equivalent or whether
107
114
values are identical.
108
115
109
- <javascript caption =" Comparison operators " >
116
+ ``` js
117
+ // Comparison operators
110
118
var foo = 1 ;
111
119
var bar = 0 ;
112
120
var baz = ' 1' ;
@@ -123,4 +131,4 @@ foo === parseInt(baz); // returns true
123
131
foo > bim; // returns false
124
132
bim > baz; // returns true
125
133
foo <= baz; // returns true
126
- </ javascript >
134
+ ```
0 commit comments