CodeBlanks Logo Icon CodeBlanks

Your first program

Learn to run a script with node and use console.log.

Your first program

In Node.js, a program is written in a file with the .js extension. For example hello.js. To run the file, you type node hello.js in the terminal.

To print something to the screen, you use console.log(...). The value inside the parentheses is printed and a newline is added automatically at the end.

This is the most basic Node.js output. Let us try it now.

Running a file

To run a JavaScript file, you use the node command in the terminal. For example, node hello.js executes the hello.js file and runs all the code inside it in order.

If the file does not exist, Node.js gives an error message. If it exists, the content is read, compiled by V8 and executed; console.log calls appear in the terminal.

This simple flow is the basis of everyday Node.js use.

Practice Print two lines

Write two console.log calls: print Hello on the first line and Node.js on the second.

Starter code

index.js
// Print two lines

Sample solution

index.js
console.log("Hello");
console.log("Node.js");

Expected output

Hello
Node.js

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 console.log call

Question
To print 'Hello' to the screen: ____("Hello");
Answer
To print 'Hello' to the screen: console.log("Hello");

Explanation: console.log prints to the screen.

2 What does the node command do?

  • Loads and runs the JavaScript file Correct answer
  • Packages the JavaScript file
  • Opens the JavaScript file in a browser
  • Deletes the JavaScript file

Explanation: The node command loads and runs the file.

3 Match the program parts

Term Meaning
hello.js Source file
node Run command
console.log Output writing
.js File extension

All lessons in Beginner