1.4 JS in a File - Video Tutorials & Practice Problems
Video duration:
2m
Play a video:
Video transcript
<v Instructor>We've gotten a good start now with the REPL.</v> Both inside the browser and at the command line. And it's really useful to build up programs by interactively entering commands and seeing what the results are. But, pretty much all real programming happens, as it did with the index guide HTML file, in files. Edited with a text editor. So we're gonna start with a program called hello.js. And we're going to output hello world from a JavaScript file. So, let's start with this. I'd like to open the whole project, as I've mentioned previously. Atom dot. What happened there? There it is. There it is. Just needed a second to refresh. All right. Okay, here is hello.js. We've seen the command a couple times. It's exactly the same thing inside a file. Console dot log. Hello world. Semicolon, can save it. And then at the command line, we can execute this using the same node command that we use to enter the REPL. We can give this an argument, which is the name of the file. Hello.js. And, there we go. Hello world. So you can see it didn't actually show us that undefined that we saw before in the REPL. That's because by default, when you run a program like this, it doesn't actually output the return values. It just outputs things, like side effects, here. So for example, if I have the number one, just the valid statement. Doesn't do anything. It doesn't show up. You would have to console.log it. So there we go. We've got now an index file, two different REPLs. And a JavaScript file. There's only one thing left to do, which is to make a shell script.