|
1 | 1 | # javascript
|
2 | 2 |
|
| 3 | +javascript is a programming language |
| 4 | +you can use to program web pages |
| 5 | +and command-line tools and servers |
| 6 | + |
3 | 7 | ---
|
4 | 8 |
|
5 |
| -# links |
| 9 | +# install node |
6 | 10 |
|
7 |
| -http://jsforcats.com/ |
| 11 | +We'll be using node on the command-line to learn javascript. |
| 12 | + |
| 13 | +Download and install node from: |
| 14 | + |
| 15 | +https://nodejs.org |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +# write a program |
| 20 | + |
| 21 | +Now let's make a program. Put this text into a file called robot.js: |
| 22 | + |
| 23 | +``` js |
| 24 | +console.log('BEEP BOOP') |
| 25 | +``` |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +# run a program |
| 30 | + |
| 31 | +Now use `node` to run your program: |
| 32 | + |
| 33 | +``` |
| 34 | +$ node robot.js |
| 35 | +BEEP BOOP |
| 36 | +``` |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +# recap: console.log |
| 41 | + |
| 42 | +`console.log(...)` is a function that prints its arguments to |
| 43 | +stdout followed by a newline (`\n`) character |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +# recap: 'BEEP BOOP' |
| 48 | + |
| 49 | +* `'BEEP BOOP'` is a string. |
| 50 | +* `'BEEP BOOP'` is an argument to the `console.log()` function. |
| 51 | + |
| 52 | +Note the single quotes on each side. |
| 53 | + |
| 54 | +You can use double quotes too: `"BEEP BOOP"` |
| 55 | + |
| 56 | +Sometimes an "argument" is called a "parameter". |
| 57 | + |
| 58 | +--- |
| 59 | + |
| 60 | +# statements |
| 61 | + |
| 62 | +In the previous program, we had a single statement. |
| 63 | +You can have multiple statements, each on their own line: |
| 64 | + |
| 65 | +``` js |
| 66 | +console.log('Such') |
| 67 | +console.log('wow.') |
| 68 | +``` |
| 69 | + |
| 70 | +Now save this code to a file `suchwow.js` and run it with `node`: |
| 71 | + |
| 72 | +``` |
| 73 | +$ node suchwow.js |
| 74 | +Such |
| 75 | +wow. |
| 76 | +``` |
| 77 | + |
| 78 | +--- |
| 79 | + |
| 80 | +# statements |
| 81 | + |
| 82 | +You might also see programs that use semicolons to separate |
| 83 | +statements: |
| 84 | + |
| 85 | +``` js |
| 86 | +console.log('Such'); |
| 87 | +console.log('wow.'); |
| 88 | +``` |
| 89 | + |
| 90 | +Semicolons are optional. There are some cases where they are |
| 91 | +necessary, but this is rare in practice. |
| 92 | + |
| 93 | +Both including and omitting semicolons are popular code styles. |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +# variables |
| 98 | + |
| 99 | +Another kind of statement is a variable declaration: |
| 100 | + |
| 101 | +``` js |
| 102 | +var x = 5 |
| 103 | +var y = 111 |
| 104 | +console.log(x * y) |
| 105 | + |
| 106 | +var a = 'beep' |
| 107 | +var b = 'boop' |
| 108 | +console.log(a + b) |
| 109 | +``` |
| 110 | + |
| 111 | +which prints: |
| 112 | + |
| 113 | +``` |
| 114 | +$ node vars.js |
| 115 | +555 |
| 116 | +beepboop |
| 117 | +``` |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +# variables |
| 122 | + |
| 123 | +Variables are containers that can hold values. |
| 124 | + |
| 125 | +Strings like `'beep'` and numbers like `5` are values. |
| 126 | + |
| 127 | +--- |
| 128 | + |
| 129 | +# operators |
| 130 | + |
| 131 | +We used the `*` operator to multiply two numbers. |
| 132 | + |
| 133 | +Here are some other operators you can try with numbers: |
| 134 | + |
| 135 | +* `+` - add |
| 136 | +* `-` - subtract |
| 137 | +* `*` - multiply |
| 138 | +* `/` - divide |
| 139 | +* `%` - modulo (the remainder after division) |
| 140 | + |
| 141 | +modulo is really handy when you need a value to "wrap around" |
| 142 | + |
| 143 | +--- |
| 144 | + |
| 145 | +# assignment updates |
| 146 | + |
| 147 | +You can change the value stored in a variable at any time by |
| 148 | +assigning a new value. |
| 149 | + |
| 150 | +``` js |
| 151 | +var x = 5; |
| 152 | +x = x + 10; |
| 153 | +console.log(x); |
| 154 | +``` |
| 155 | + |
| 156 | +which prints `15`: |
| 157 | + |
| 158 | +``` |
| 159 | +$ node x.js |
| 160 | +15 |
| 161 | +``` |
| 162 | + |
| 163 | +--- |
| 164 | + |
| 165 | +# assignment operators |
| 166 | + |
| 167 | +There is a shorthand for running an operator and saving the |
| 168 | +result back to a variable. Instead of `x = x + 10` we can do |
| 169 | +`x += 10` instead: |
| 170 | + |
| 171 | +``` js |
| 172 | +var x = 5 |
| 173 | +x += 10 |
| 174 | +console.log(x) |
| 175 | +``` |
| 176 | + |
| 177 | +Just take one of the arithmetic operators from earlier and add an |
| 178 | +`=` at the end to do an update "in place": |
| 179 | + |
| 180 | +* `+=` - add in place |
| 181 | +* `-=` - subtract in place |
| 182 | +* `*=` - multiply in place |
| 183 | +* `/=` - divide in place |
| 184 | +* `%=` - modulo in place |
| 185 | + |
| 186 | +--- |
| 187 | + |
| 188 | +# increment, decrement |
| 189 | + |
| 190 | +There are two other operators for convenience: |
| 191 | + |
| 192 | +* `x++` - increment x by 1 in place |
| 193 | +Same as: `x += 1` or `x = x + 1` |
| 194 | + |
| 195 | +* `x--` - decrement x by 1 in place |
| 196 | +Same as: `x += 1` or `x = x + 1` |
| 197 | + |
| 198 | +--- |
| 199 | + |
| 200 | +--- |
| 201 | + |
| 202 | +--- |
| 203 | + |
| 204 | +# arrays |
| 205 | + |
| 206 | +--- |
| 207 | + |
| 208 | +# |
| 209 | + |
| 210 | +--- |
| 211 | + |
| 212 | +# objects |
| 213 | + |
| 214 | +We've already been using some objects: |
| 215 | + |
| 216 | +* `process` is an object with a property `argv` that is an array. |
| 217 | +* `console` is an object with a property `log` that is a function. |
| 218 | + |
| 219 | +--- |
| 220 | + |
| 221 | +# functions |
| 222 | + |
| 223 | + |
| 224 | + |
| 225 | +--- |
| 226 | + |
| 227 | +# builtins |
| 228 | + |
| 229 | + |
| 230 | + |
| 231 | +--- |
| 232 | + |
| 233 | +# while loop |
| 234 | + |
| 235 | +--- |
| 236 | + |
| 237 | +# for loop |
| 238 | + |
| 239 | +--- |
8 | 240 |
|
9 | 241 | # homework
|
10 | 242 |
|
| 243 | +Your homework is to get through the `javascripting` lesson on |
| 244 | +nodeschool: |
| 245 | + |
11 | 246 | http://nodeschool.io/#javascripting
|
12 | 247 |
|
| 248 | +--- |
| 249 | + |
| 250 | +# links |
| 251 | + |
| 252 | +http://jsforcats.com/ |
| 253 | + |
| 254 | +--- |
| 255 | + |
0 commit comments