|
| 1 | +# REVIEW JavaScript Basics |
| 2 | + |
| 3 | +## Variables |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +### Variable declaration |
| 8 | + |
| 9 | +Variables are "declared" using the `var`, `let` or `const` keyword: |
| 10 | + |
| 11 | +```js |
| 12 | +var x; |
| 13 | +let foo; |
| 14 | +const bar; |
| 15 | +``` |
| 16 | + |
| 17 | +### let and const |
| 18 | +- read about [let](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let) |
| 19 | +- read about [const](https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Statements/const) |
| 20 | +- [let vs const] (http://wesbos.com/let-vs-const/) |
| 21 | + |
| 22 | +>TODO - reconsider resources. |
| 23 | +
|
| 24 | +Here, we say: "declare variable x and initialize it with the integer (number) 5". |
| 25 | + |
| 26 | +```js |
| 27 | +let foo; // declare variable `foo` |
| 28 | +let foo = 6; // declare and assign a variable at the same time |
| 29 | +``` |
| 30 | + |
| 31 | +You can also assign a value to an existing variable: |
| 32 | +```js |
| 33 | +foo = 4; // change variable `foo` |
| 34 | + |
| 35 | +``` |
| 36 | + |
| 37 | + |
| 38 | +Basic Data types [Strings, Numbers, Arrays] |
| 39 | + |
| 40 | +## Variable types |
| 41 | + |
| 42 | +All variables have a type. In our example above, the variable `x` is a `number`. JavaScript supports the following types: |
| 43 | + |
| 44 | +* `string`, e.g. "HackYourFuture" |
| 45 | +* `number`, e.g. 5, or 10.6 |
| 46 | +* `boolean`, e.g. `true` or `false` |
| 47 | +* `array`\*, e.g. `[1, 2, 3]` or `['what', 'is', 'your', 'name']` |
| 48 | +* `object`, e.g. `{name: 'John', age: 24}`, or the special object `null` |
| 49 | +* `function`, e.g. `function () { return 4; }` |
| 50 | +* `symbol` |
| 51 | + |
| 52 | +In addition, a variable may be `undefined`. This is also a special type. |
| 53 | + |
| 54 | +To get the type of a variable, use the following code: |
| 55 | + |
| 56 | +```js |
| 57 | +var x = 5; |
| 58 | +var typeOfX = typeof x; // -> "number" |
| 59 | +``` |
| 60 | + |
| 61 | +Note that I've put an asterisk behind 'array'. That is because in JavaScript, array is a special kind of object: |
| 62 | + |
| 63 | +```js |
| 64 | +var arr = [1, 2, 3]; |
| 65 | +var typeOfArr = typeof arr; // -> "object" |
| 66 | +``` |
| 67 | + |
| 68 | +However, in our communication, we will call these variables arrays. |
| 69 | + |
| 70 | +### Null & undefined |
| 71 | + |
| 72 | +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". |
| 73 | + |
| 74 | +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. |
| 75 | + |
| 76 | +```js |
| 77 | +var x; |
| 78 | +console.log(typeof x); // -> "undefined" |
| 79 | +``` |
| 80 | + |
| 81 | + |
| 82 | +## Arrays |
| 83 | + |
| 84 | +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". |
| 85 | + |
| 86 | +When you want to access an element inside an array, you use an "index". This is the number that you put between brackets (`[]`). |
| 87 | + |
| 88 | +Given the following code: |
| 89 | + |
| 90 | +```js |
| 91 | +var arr = ['john', 'jane', 'jack']; |
| 92 | +console.log(arr[0]); |
| 93 | +``` |
| 94 | + |
| 95 | +The number `0` is the "index of the first element of array `arr`". Conversely, the element "at index 0 in array `arr` is `'john'`". |
| 96 | + |
| 97 | +Instead of a number, you can also use a variable to access elements in an array, *as long as this variable is a number*: |
| 98 | + |
| 99 | +```js |
| 100 | +var arr = ['john', 'jane', 'jack']; |
| 101 | +var a = 1; |
| 102 | +console.log(arr[a]); // -> jane |
| 103 | +``` |
| 104 | + |
| 105 | +If the index you use is not an integer (a whole number), or if it's less than `0` or if it's greater than or equal to the array's length, you will get back `undefined`. |
| 106 | + |
| 107 | + |
0 commit comments