You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Week1/REVIEW.md
+89-44Lines changed: 89 additions & 44 deletions
Original file line number
Diff line number
Diff line change
@@ -2,10 +2,10 @@
2
2
3
3
```
4
4
This review covers:
5
-
• some commands thought by Unmesh in class today
5
+
• some commands taught by Unmesh in class today
6
6
• Intro JavaScript (What is it, where can you use it for)
7
-
• Variables [var, let, const]
8
-
• Basic Data types [Strings, Numbers, Arrays]
7
+
• Variables [var, let, const]
8
+
• Basic Data types [Strings, Numbers, Arrays]
9
9
• Operators
10
10
```
11
11
@@ -40,7 +40,7 @@ man <COMMAND> : Display manual of the COMMAND
40
40
41
41
## Variables
42
42
43
-
A "variable" is a place where you can store information, such as a string, or a number. New variables in JavaScript are declared using one of three keywords: let, const, or var.
43
+
A "variable" is a place where you can store information, such as a string, or a number. A variable has a _name_ (that you choose) and a _value_. New variables in JavaScript are declared using one of three keywords: `let`, `const`, or `var`.
44
44
45
45
> Think of variables names like **labels** on boxes, while the value of the variable are the **contents** of the box - you could change the contents of a box and leave the label intact, the contents of the boxes can have different types, the boxes should have good labels (a box of books being labeled pens would be very confusing),
46
46
>
@@ -50,20 +50,26 @@ A "variable" is a place where you can store information, such as a string, or a
50
50
51
51
### Variable declaration
52
52
53
-
Variables are "declared" using the `var`, `let` or `const` keyword:
53
+
Variables are "declared" using the `var`, `let` or `const` keyword. In the following example three variables are declared with the names `x`, `foo` and `bar`.
54
54
55
55
```js
56
56
var x;
57
57
let foo;
58
58
constbar;
59
59
```
60
60
61
+
Note that the chosen names in this example are meaningless (perhaps with the exception of `x`, for instance as part of a mathematical program). You should make an effort to always choose names that best describe what you intend this variable to hold.
62
+
63
+
### var
64
+
65
+
Prior to JavaScript ES6 the `var` keyword was the only way to declare a variable. ES6 introduced two new keywords, `let` and `const` for declaring variables. They improve on how the older `var` declaration works (this involves the concept of "scope" that you will learn about in the third lecture). In HackYourFuture we encourage you to use the more modern `let` and `const` keywords over `var`, but you will often come across `var` in existing books, software libraries and examples on the Internet, so you should understand `var` too.
66
+
61
67
### let and const
62
68
- read about [let](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let)
63
69
- read about [const](https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Statements/const)
64
-
-[let vs const] (http://wesbos.com/let-vs-const/)
70
+
-[let vs const] (http://wesbos.com/let-vs-const/)
65
71
66
-
Here, we say: "declare variable x and initialize it with the integer (number) 5".
72
+
Here, we say: "declare variable x and initialize it with the integer (number) 6".
All variables have a type. In our example above, the variable `x` is a`number`. JavaScript supports the following types:
90
+
Values are the "things" that you assign to a variable. All values have a type. In our example above, the variable `x` is assigned a value of type`number`. JavaScript supports the following types:
85
91
86
92
*`string`, e.g. "HackYourFuture"
87
93
*`number`, e.g. 5, or 10.6
@@ -90,10 +96,11 @@ All variables have a type. In our example above, the variable `x` is a `number`.
90
96
*`object`, e.g. `{name: 'John', age: 24}`, or the special object `null`
91
97
*`function`, e.g. `function () { return 4; }`
92
98
*`symbol`
99
+
*`undefined`
93
100
94
-
In addition, a variable may be `undefined`. This is also a special type.
101
+
If you declare a variable without specifying its value, then, by default its value is `undefined`.
95
102
96
-
To get the type of a variable, use the following code:
103
+
To get the type of a value assigned to a variable, use the following code:
97
104
98
105
```js
99
106
let x =5;
@@ -107,38 +114,39 @@ let arr = [1, 2, 3];
107
114
let typeOfArr =typeof arr; // -> 'object'
108
115
```
109
116
110
-
However, in our communication, we will call these variables arrays.
117
+
However, in practice we will call these variables arrays.
111
118
112
-
### Null & undefined
119
+
### Null and undefined
113
120
114
-
The values `null` and `undefined` are very similar in JavaScript, but they behave a bit differently. The difference is that `null` always has type "object", and `undefined` always has type "undefined".
121
+
The values `null` and `undefined` are very similar in JavaScript, but they behave a bit differently. The difference is that `null` always has type `'object'`, and `undefined` always has type `'undefined'`.
115
122
116
-
Whenever you declare a variable, but you don't set a value, the variable will become `undefined`. JavaScript will never make a variable `null` unless you explicitly program it.
123
+
Whenever you declare a variable, but you don't set a value, the variable will become `undefined`. JavaScript will never make a variable `null` unless you explicitly assign it the value `null`.
117
124
118
125
```js
119
126
let x;
120
127
console.log(typeof x); // -> 'undefined'
121
128
```
122
129
123
130
124
-
### Typeof
131
+
### `typeof` operator
125
132
126
-
You can use `typeof` to get the type of a certain variable as you have seen in the above section 'Variable types'. As you can see in the following examples it returns the type of data that you have stored in your variable.
133
+
You can use the `typeof`operator to get the type of a certain variable as you have seen in the above section 'Value types'. As you can see in the following examples it returns the type of value that you have assigned to your variable.
127
134
128
135
## Strings
129
136
130
-
In JavaScript you can store a series of characters inside a variable, you then call this a string. You can store all sorts of characters (text/numbers, spaces or phrases) in strings. By using the `''` you define that something is a string. You can also use `""` to create a string. Both are fine as long as you are consistent (just make a choice on which one you prefer and stick to it).
137
+
In JavaScript you can assign a series of characters to a variable, you then call this a string. You can use all sorts of characters (text/numbers, spaces or phrases) in strings. By using the `''` you define that something is a string. You can also use `""` to create a string. Both are fine as long as you are consistent (just make a choice on which one you prefer and stick to it).
131
138
132
139
```js
133
140
let foo ='42';
134
141
typeof foo //-> 'string'
135
142
136
-
let bar ='I\'m 99 years old ';
143
+
let bar ='I\'m 99 years old ';
137
144
typeof bar //-> 'string'
138
145
```
139
146
140
147
### String indexes and string properties
141
-
The index of a string always starts at 0.
148
+
149
+
Individual characters in a string can be accessed by their position (index) within the string. The index of a string always starts at 0.
142
150
Strings also have properties, for example `.length` you can use this to find the length of a string.
143
151
144
152
So for example:
@@ -150,14 +158,30 @@ baz.length; //-> 11
150
158
151
159
### String methods
152
160
153
-
>Todo
161
+
String methods are named operations that you can use on string values to create new values. For example, the `toUpperCase` method creates a new string with all uppercase letters.
162
+
163
+
```js
164
+
let baz ='Hello World!';
165
+
baz.toUpperCase(); // -> 'HELLO WORLD'
166
+
```
167
+
168
+
Methods differ from properties (such as `.length`) in that you must always use them with open and close parentheses `(` and `)`.
169
+
170
+
Some methods need additional information, and you must supply it in the form of one or more _parameters_. For example:
171
+
172
+
```js
173
+
let baz ='Hello World!';
174
+
baz.slice(3, 8) // -> 'lo Wo'
175
+
baz.startsWith('He') // -> true
176
+
baz.indexOf('World') // -> 6
177
+
```
154
178
155
179
## Numbers
156
180
157
-
All numbers in JavaScript are considered numberswith or without decimal
181
+
All numbers in JavaScript are considered numbers, either with or without a decimal.
158
182
159
183
```js
160
-
let quux =42;
184
+
let quux =42;
161
185
typeof quux //-> 'number'
162
186
163
187
let quuux =3.3333;
@@ -168,14 +192,14 @@ typeof quuux //-> 'number'
168
192
169
193
## Arrays
170
194
171
-
Variables that are arrays contain a list of things, instead of just one thing. What's inside the array, we typically call "elements". So, the array `[1, 2, 3]` has three elements. The array `[]` has no elements and is therefore empty. The number of elements in an array is called its "length".
195
+
Arrays are values that contain a list of things, instead of just one thing. What's inside the array, we typically call "elements". So, the array `[1, 2, 3]` has three elements. The array `[]` has no elements and is therefore empty. The number of elements in an array is called its "length".
172
196
173
197
When you want to access an element inside an array, you use an "index". This is the number that you put between brackets (`[]`).
174
198
175
199
Given the following code:
176
200
177
201
```js
178
-
var arr = ['john', 'jane', 'jack'];
202
+
let arr = ['john', 'jane', 'jack'];
179
203
console.log(arr[0]);
180
204
```
181
205
@@ -193,17 +217,20 @@ If the index you use is not an integer (a whole number), or if it's less than `0
193
217
194
218
More about [arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)
195
219
220
+
## Operators
221
+
196
222
### Comparison operators
197
223
198
224
>Note the two different uses of the equals sign:
199
225
A single equals sign (=) is used to assign a value to a variable.
200
226
A triple equals sign (===) is used to compare two values (see Equality Operators).
@@ -219,13 +246,16 @@ How does this work in practice?
219
246
220
247
> why does `7 == '7'` returns true and `9 === '9'` returns false?
221
248
249
+
We strongly recommend that you always use the strict form when comparing for equality (`===`) or inequality (`!==`). Use the non-strict forms only when there is a compelling reason to do so (you will be hard pressed to find such a reason).
250
+
222
251
#### Relational operators
252
+
223
253
* Greater than operator `>`
224
254
* Greater than or equal operator `>=`
225
255
* Less than operator `<`
226
256
* Less than or equal operator `<=`
227
257
228
-
```js
258
+
```js
229
259
4>3// -> true
230
260
3>=3// -> true
231
261
13<12// -> false
@@ -239,49 +269,64 @@ More about [comparison operators](https://developer.mozilla.org/en-US/docs/Web/J
239
269
* Addition `+`
240
270
* Subtraction `-`
241
271
* Multiplication `*`
242
-
* Division `/`
243
-
* Remainder (sometimes called modulo) `%`
272
+
* Division `/`
273
+
* Remainder (sometimes called modulo) `%`
244
274
<br>Returns the remainder left over after you've shared the left number out into a number of integer portions equal to the right number.
245
275
246
276
```js
247
-
8+9// -> 17, adds two numbers together.
277
+
8+9// -> 17, adds two numbers together.
248
278
20-12// -> 8, subtracts the right number from the left.
249
279
3*4// -> 12, multiplies two numbers together.
250
-
10/5// -> 2, divides the left number by the right.
280
+
10/5// -> 2, divides the left number by the right.
251
281
8%3/// -> 2, as three goes into 8 twice, leaving 2 left over.
252
282
```
253
283
254
-
More about [Arithmetic_Operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#.25_.28Modulus.29)
284
+
More about [Arithmetic operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#.25_.28Modulus.29)
255
285
256
286
### Logical operators
257
287
258
288
* AND `&&`
259
-
* OR `||`
289
+
* OR `||`
260
290
* NOT `!`
261
291
262
292
Given that x = 6 and y = 3
263
293
```js
264
294
x <10&& y >1// -> true
265
-
x ==5|| y ==5// -> false
295
+
x ===5|| y ===5// -> false
266
296
x !== y // -> true
267
297
```
268
298
299
+
Logical NOT
300
+
301
+
```js
302
+
true===!false
303
+
false===!true
304
+
```
305
+
269
306
More about [logical operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators)
270
307
271
-
### Operator precedence
308
+
### typeof operator
309
+
310
+
We already mentioned the `typeof` operator:
311
+
312
+
```js
313
+
typeof5// -> 'number'
314
+
```
315
+
316
+
### Assignment operators
272
317
273
-
There are compound assignment operators such as +=. The following two assignments are equivalent:
318
+
In addition to the simple assignment operator `=` there are also compound assignment operators such as `+=`. The following two assignments are equivalent:
0 commit comments