Your first JavaScript program

A JavaScript program is a list of statements in a file, and the smallest useful one has a single statement: a call to console.log with a string. There is no class to declare, no function to name and no return value to write. Save the file, name it on the command line, and the line appears in the terminal.

This lesson writes that file, reads it line by line, adds a second program that builds its text from values, and ends with the one number a shell cares about after a run: the exit status. All output on the page is the output of the files shown, on the machine named at the end.

Hello from a file

Type the line below into a new file, save it as hello.js in the folder you opened in the editor, and run it with node hello.js. The runtime reads the file from top to bottom, runs each statement, and exits when the last one is done.

console.log('Hello, JavaScript');

node hello.js

Hello, JavaScript

The line is printed to standard output. There is no prompt, no banner and no timing information: a program that prints one line prints one line, and that is the whole contract of the runtime with the terminal.

The file line by line

console is an object that every runtime provides, and log is a function on it. The parentheses call the function, and the value between them is its argument. A string is written in single or double quotes; both are the same type, and the course uses double quotes in the files and single quotes in this prose.

The second program keeps two values in two bindings and builds its lines from them. const declares a binding that may not be pointed at another value, ${...} inside a backtick string inserts the value, and the backtick form is called a template literal. The semicolons end the statements, and the next lesson explains when they may be left out.

const name = 'JavaScript'; const year = 1995; console.log(`Hello, ${name}`); console.log(`The language appeared in ${year}`);

node greeting.js

Hello, JavaScript
The language appeared in 1995

The argument of console.log is a string in both lines, but only the second program builds it at run time. Backticks are not needed for a plain string, and a template literal without ${...} is an ordinary string with a different pair of quotes.

The exit status

Every process returns a number to the shell when it ends. Zero means success, and anything else is a failure that a script can test. A JavaScript program sets that number with process.exitCode, and the runtime returns it after the last statement has run. The difference from process.exit() is that the assignment lets the program finish what it was doing.

console.log('work done'); process.exitCode = 3;

node exitcode.js
echo $?

work done
3

Both lines of the program ran, and then the shell printed the number. A command that ends a script with process.exit(3) returns the same 3, but it stops at the call: pending output may be cut and callbacks do not run.

The terminal and the editor

The loop of the whole course is three steps: change the file in the editor, run node on it in the terminal, read what came back. The terminal keeps the working folder, so node hello.js finds the file only when the folder with the file is the one the shell is in. pwd prints that folder, and ls lists what is in it.

When the shell answers that it cannot find the file, the file is usually there and the folder is wrong. When the runtime answers with a SyntaxError, the file was found and one line of it does not parse; the message names that line. The two failures look different on purpose, and telling them apart saves more time than any tool.

Exercises

  1. Change hello.js so that it prints two lines instead of one. How many statements does the file have now, and how many times is console.log called?
  2. In greeting.js, replace the template literal with a string built by +. Does the output change, and which form is easier to read with three values?
  3. Set process.exitCode to 0 in exitcode.js and run the two commands again. What does the shell print, and what would a script that tests the status conclude?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does console.log("Hello, JavaScript") do?

    Answer

    Writes the string and a newline to standard output

    log writes its arguments to standard output and ends the line; its own return value is undefined.

  2. Which command runs the file hello.js?

    Answer

    node hello.js

    The runtime takes the path of the script as its first argument.

  3. What is a template literal?

    Answer

    A string in backticks that may contain ${...} expressions

    Backticks allow ${...} inside the string, and the expression is evaluated when the line runs.

  4. What is the exit status of a successful run?

    Answer

    0

    Zero means success, and any other number is a failure that a shell script can test.

  5. What does this program print? ``const name = "JavaScript"; console.log(Hello, ${name});``

    Answer

    Hello, JavaScript

    The ${...} part is replaced by the value of the binding, and nothing else is added.

  6. What does process.exitCode = 3 do?

    Answer

    Sets the number the process returns when it ends

    The assignment sets the status and the program continues; process.exit(3) is the call that stops it.

  7. What does this program print, and what is its exit status? console.log("work done"); process.exitCode = 3;

    Answer

    work done, then status 3

    The line runs, the status is set, and the runtime returns 3 when the program ends normally.

  8. The shell answers that it cannot find hello.js, but the file is in the folder. What is the cause?

    Answer

    The shell is in another folder

    node resolves a relative path against the working folder of the shell, and pwd shows which folder that is.

  9. How many statements does the hello.js of this lesson have?

    Answer

    One

    The file is a single console.log call, and the runtime needs nothing else.

  10. Which pair of quotes can hold a string in JavaScript?

    Answer

    Single quotes and double quotes

    Single quotes and double quotes are the same type; backticks add interpolation, and brackets are not quotes.

The code on this page was run with Node.js 24.22.1 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image