Skip to content
Lesson 1 of 15

Step 1 of 5 · Reading · ~3 min

Learn

Getting Started

Hello, World

By the end of this lesson you can print exact lines of text from a JavaScript program and read one line of input. Every exercise in this course is a round trip through those two things: input in, exact output out.

How your code runs

When you press Run, your file is executed by Node.js, which is the JavaScript engine from the browser packaged as a command-line program. Whatever the program writes to standard output (stdout) comes back to you, and the grader compares it to the expected output character by character. Printing exactly the right text is how you pass.

console.log

console.log writes its arguments to stdout and ends the line for you:

javascript

Text must be inside quotes. A piece of text in quotes is a string. Without quotes, JavaScript reads the word as the name of a variable:

javascript

Joining pieces

There are two ways to put several pieces on one line, and they differ in one character:

javascript

Use the comma form when you want a space and + when you need to attach punctuation directly, because console.log("Rain", "!") prints Rain ! with a stray space.

javascript

const city = "Oslo"; stores a string under a name so you can reuse it. The next lesson explains declarations properly.

Comments

Everything after // on a line is ignored by JavaScript. Comments are notes for humans, and the starters use them to mark where your code goes.

Reading one line of input

Most exercises give your program text on standard input (stdin). The starters read all of it with one line you do not have to write:

javascript

readFileSync(0, ...) reads everything the program was given as one string, and .trim() removes the newline (and any spaces) at both ends. If the input is the single line Oslo, then input holds "Oslo".

Common mistakes

  • Printing almost-right text. Hello,World (no space), hello, World (lowercase) and a missing final ! all fail, because the grader does not judge intent.
  • Using a comma where the output needs no space. console.log(name, "!") prints Ada !, but you wanted Ada!.
  • Forgetting the quotes, so a word like Welcome is read as a variable and the program crashes with a ReferenceError.
  • Computing the right text but never calling console.log, so nothing is printed at all.

Your exercise

Read a person's name from the input and print two greeting lines for them. The starter already reads the name into name. Watch the punctuation that must touch the name, and remember that a name can contain spaces.

Up nextVariables: let and constGetting Started

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…