Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
When you type global, it's going to evaluate that global object and show you everything that's a part of it. It prints it out in some nice colors and formatting so you can follow along. One of the global's is require. So if I type require, it's going to evaluate the required global. This shows me that require is a function and it has several properties and methods attached to it. Now let's start settings and variables. And one thing to keep in mind as we're using the REPL. Even though we're issuing one line of code at a time, any variables we set are going to stay in memory.
This makes it possible for us to set a variable now and then use it in the next command or any command after that. So let's start setting some variables. First I'm going to set a to 10. So after I set a to 10, it evaluates a and spits back 10. Now I'm going to set b to 20. And now I want the REPL to evaluate a plus b. So notice we haven't changed the values of a or b. If I evaluate a, it's still going to be 10. And if I evaluate b, it's still going to be 20.
I can also do b minus a. But if I try to evaluate a variable I have not set, I'm going to get undefined. So lets try evaluating c. So it now gives me a reference error that c is not defined, because I haven't set it to anything yet. Lets set c to the string flight. So now that variable is set and it spits back flight. And now let's see what happens when I try to add a and c. So you'll notice here that it treats a as a string because c is a string.
If there's no way to mathematically add the two variables, it's just going to treat them both as strings and concatenate them. Now if I try to do a divided by c, it's going to give me not a number as an error. That's because although a was a number, c is a string, and dividing a number by a string is an invalid operation. We can also set variables as functions. So now I'm going to set the variable d, and I'm going to set this to a function. Its going to take one argument called value. And all I'm going to do is output the value plus 10.
So I'm going to go console.log, and then the value plus 10, and then I'm just going to finish off this function. So it's now stored that function. It evaluated d and says it's a function, so it printed that out on the screen. And now I'm going to call this function, so now I'm going to call d and pass in a. So you'll notice we got two things back, we got 20, which is 10 plus 10, and then we got undefined. It's showing undefined because this function is not returning any values.
We can also just set this so that we return a number. I'm pressing up twice again and this time I'm going to remove all of this code and now I'm just going to return value plus 10. So let's call d with a again. And now it's just returning 20. Node's REPL is a good place to go when you want to run code one line at a time. It's also good for inspecting variables as you create and modify them. In the next video, we'll look at how to create a custom REPL.
There are currently no FAQs about Node.js Essential Training.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.