Computer >> Computer tutorials >  >> Programming >> Javascript

JavaScript – Hello World

One of the very first programs you can write to make sure your Node version works is the “Hello World” program. There are two ways to create the program:  in the terminal or in your code editor of choice (i.e. Visual Studio Code, Vim, etc.). Make sure you have Node installed on your machine to get started.

Terminal

1.In your terminal, type node to start Node. 

JavaScript – Hello World

2.Input console.log(“Hello World”) into the Interpreter and hit enter. 

3.Hello World will show up on the next line! Don’t worry if it says “undefined” in the following line. That just means we haven’t returned anything from a function. 

JavaScript – Hello World

4.If you would like to create a function to test, you can do that too! Here’s how to do that in the terminal:

JavaScript – Hello World

Node will store the function in memory. If you want to run it, just type helloWorld() in the terminal and hit enter. It’ll return Hello World! in the console. 

Code Editor (i.e. Visual Studio Code)

  1. Open up your editor of choice and create a new file and save it as  hello.js.
  2. In your file, input console.log(“Hello World”) into the file and save it. 
  3. In your terminal, navigate to the directory/folder you saved your file. Use cd and ls to navigate from directory to directory. 
  4. Use node hello.js to run the file. Hello World should show up on the next line. 
  5. If you are using VS Code, the “play” button in the upper right hand corner will also run the current file and show the output on the lower half of the screen. 

That’s it! You have successfully created a simple program in JavaScript that shows that your Node environment works.