Skip to content

Commit 9f9fe70

Browse files
committed
Updated REPL lab
1 parent 48cac55 commit 9f9fe70

1 file changed

Lines changed: 34 additions & 17 deletions

File tree

labs/REPL.md

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ In this lab you'll learn how to use the Node REPL to execute ad-hoc JavaScript s
2020

2121
## Starting the Node REPL
2222

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:
2424

2525
```JavaScript
2626
> 1 + 5
@@ -31,24 +31,24 @@ undefined
3131
6
3232
```
3333

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.
3535

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 ```_```.
3737

3838
```JavaScript
3939
> "Node Rocks!"
40-
"Node Rocks!"
40+
'Node Rocks!'
4141
> _
42-
"Node Rocks!"
42+
'Node Rocks!'
4343
> var lastResult = _
4444
undefined
4545
> lastResult
46-
"Node Rocks!"
46+
'Node Rocks!'
4747
```
4848

4949
## Arrays
5050

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.
5252

5353
* The array Initializer:
5454

@@ -71,7 +71,7 @@ Adding an item to an array:
7171

7272
```JavaScript
7373
> var a = ['apple', 'banana', 'kiwi']
74-
['apple', 'banana', 'kiwi']
74+
undefined
7575
> a.length
7676
3
7777
> a.push("lemon")
@@ -80,11 +80,20 @@ Adding an item to an array:
8080
5 //unshift adds an element to the beginning of the array and returns the new length
8181
```
8282

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.
8484

8585
```JavaScript
8686
> 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
8897
> a.pop()
8998
'lemon' //pop removes and returns the last value in the array.
9099
> a.shift()
@@ -121,7 +130,9 @@ undefined
121130
3
122131
```
123132

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.
125136

126137
```JavaScript
127138
> o['foo']
@@ -148,18 +159,23 @@ Objects can be composed of other objects:
148159

149160
## Functions
150161

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:
152163

153164
```JavaScript
154-
var Foo = function () { }
165+
> function Foo () {}
166+
undefined
167+
> Foo
168+
[Function: Foo]
169+
170+
> var Bar = function () {}
171+
undefined
172+
> Bar
173+
[Function]
155174
```
156175

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.
158177

159178
```JavaScript
160-
//Anonymous function declaration
161-
function () { }
162-
163179
//Declare a function that takes a callback argument
164180
var Foo = function (callback) {
165181
//Foo function body
@@ -177,6 +193,7 @@ Foo(function () {
177193
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):
178194

179195
```
196+
// Notice the missing right bracket.
180197
> var boo = function () {
181198
...
182199
```

0 commit comments

Comments
 (0)