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: labs/REPL.md
+34-17Lines changed: 34 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ In this lab you'll learn how to use the Node REPL to execute ad-hoc JavaScript s
20
20
21
21
## Starting the Node REPL
22
22
23
-
To launch the Node REPL open a command prompt or terminal and execute ```node```. Once open, evaluate a few simple expressions
23
+
To launch the Node REPL open a command prompt or terminal and execute ```node```. Once open, evaluate a few simple expressions:
24
24
25
25
```JavaScript
26
26
>1+5
@@ -31,24 +31,24 @@ undefined
31
31
6
32
32
```
33
33
34
-
Notice the result of the statement executed is printed on the following line without ```>```.
34
+
Notice the result of the statement executed is printed on the following line without ```>```. If you made a typo you can cancel your statement by pressing ```CTRL+C``` once.
35
35
36
-
If you forgot to assign the value of the previously executed statement, the Node REPL provides a useful syntax to access the previous result.
36
+
If you forgot to assign the value of the previously executed statement, the Node REPL provides a useful syntax to access the previous result through ```_```.
37
37
38
38
```JavaScript
39
39
>"Node Rocks!"
40
-
"Node Rocks!"
40
+
'Node Rocks!'
41
41
> _
42
-
"Node Rocks!"
42
+
'Node Rocks!'
43
43
>var lastResult = _
44
44
undefined
45
45
> lastResult
46
-
"Node Rocks!"
46
+
'Node Rocks!'
47
47
```
48
48
49
49
## Arrays
50
50
51
-
There are a few different ways to create arrays in JavaScript. First, you can use the array syntax, for example ```[1,3,3,7]```. This will create an array with four elements. A major difference between arrays in JavaScript and many other languages is that they are mutable and the size is not required to create. Another way of creating an array is with the ```Array``` constructor.
51
+
There are a few different ways to create arrays in JavaScript. First, you can use the array syntax, for example ```[1,3,3,7]```. This will create an array with four elements. A major difference between arrays in JavaScript and many other languages is that they are mutable and the size is not required upon creation. Another way of creating an array is with the ```Array``` constructor.
52
52
53
53
* The array Initializer:
54
54
@@ -71,7 +71,7 @@ Adding an item to an array:
71
71
72
72
```JavaScript
73
73
> var a = ['apple', 'banana', 'kiwi']
74
-
['apple', 'banana', 'kiwi']
74
+
undefined
75
75
> a.length
76
76
3
77
77
> a.push("lemon")
@@ -80,11 +80,20 @@ Adding an item to an array:
80
80
5 //unshift adds an element to the beginning of the array and returns the new length
81
81
```
82
82
83
-
Removing an item from an array:
83
+
Now inspect the contents of your array simply by typing the name of the variable and pressing enter.
84
84
85
85
```JavaScript
86
86
> a
87
-
['lime', 'apple', 'banana', 'kiwi', 'lemon']
87
+
[ 'lime',
88
+
'apple',
89
+
'banana',
90
+
'kiwi',
91
+
'lemon' ]
92
+
```
93
+
94
+
Removing an item from an array:
95
+
96
+
```JavaScript
88
97
> a.pop()
89
98
'lemon' //pop removes and returns the last value in the array.
90
99
> a.shift()
@@ -121,7 +130,9 @@ undefined
121
130
3
122
131
```
123
132
124
-
Properties using the array syntax:
133
+
Properties using the array syntax.
134
+
135
+
This syntax allows for you to create properties on objects that would otherwise be impossible to access using the dot syntax above.
125
136
126
137
```JavaScript
127
138
> o['foo']
@@ -148,18 +159,23 @@ Objects can be composed of other objects:
148
159
149
160
## Functions
150
161
151
-
JavaScript functions are declared using the ```function``` keyword.
162
+
JavaScript functions are declared using the ```function``` keyword. This will create a function called Foo that does nothing:
152
163
153
164
```JavaScript
154
-
varFoo=function () { }
165
+
>functionFoo () {}
166
+
undefined
167
+
> Foo
168
+
[Function: Foo]
169
+
170
+
>varBar=function () {}
171
+
undefined
172
+
> Bar
173
+
[Function]
155
174
```
156
175
157
-
Functions that do not have a name are *Anonymous Functions*. These are commonly used as callback arguments to other functions.
176
+
Functions that do not have a name are *Anonymous Functions*. For example, ```function () { }``` is considered anonymous. These are commonly used as callback arguments to other functions.
158
177
159
178
```JavaScript
160
-
//Anonymous function declaration
161
-
function () { }
162
-
163
179
//Declare a function that takes a callback argument
164
180
varFoo=function (callback) {
165
181
//Foo function body
@@ -177,6 +193,7 @@ Foo(function () {
177
193
The Node REPL allows for multi-line statements to be executed. When a line cannot be processed as a complete JavaScript statement the Node REPL prompts for more input (this example starts a functional closure but does not terminate it with a closing bracket):
0 commit comments