This is the second article of the series “Learn JavaScript” aimed at helping you with your first steps in the JavaScript world.

Last time, you learned how to prepare your JavaScript workspace by installing Visual Studio Code and Node.js on your computer. I’ve shown you how to write your first hello world program and if you haven’t done that yet, stop here and do it now.

In this lesson, you’ll learn how text output works in JavaScript and how to add comments to your code.

Eventually, we’ll review the common beginner mistakes related to the text output in JavaScript and Node.js text output with console.log


To print out a text to the screen in Node.js you should use the command console.log. Make sure to follow the pattern that I show you. The command console.log should be followed parenthesis, and in them, you’re placing the text that you’d like to be printed on the screen.

Remember to wrap this text in quotes.

If you don’t do that, you’ll see an error.

console.log(hello)

Output:

node index.js

/Users/coderslang/Documents/coderslang-js/index.js:1
console.log(hello)
^

ReferenceError: hello is not defined
at Object.<anonymous> (/Users/coderslang/Documents/coderslang-js/index.js:1:13)
at Module._compile (node:internal/modules/cjs/loader:1119:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1173:10)
at Module.load (node:internal/modules/cjs/loader:997:32)
at Module._load (node:internal/modules/cjs/loader:838:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:18:47

The quotes could be of 3 different types.

  1. Double quotes - "
  2. Single quotes - '
  3. Backticks - ```

Make sure to find all those characters on your keyboard and note the minor difference between the single quote and the backtick. It’s important to always start and end your strings with the same time of a quote. You can’t start a string with a backtick and end it with a single quote as it will give you the error.

console.log(`hello')
node index.js

/Users/coderslang/Documents/coderslang-js/index.js:1
console.log(`hello')

SyntaxError: Unexpected end of input
at Object.compileFunction (node:vm:360:18)
at wrapSafe (node:internal/modules/cjs/loader:1048:15)
at Module._compile (node:internal/modules/cjs/loader:1083:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1173:10)
at Module.load (node:internal/modules/cjs/loader:997:32)
at Module._load (node:internal/modules/cjs/loader:838:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:18:47

Now you can practice a little with printing different messages to the console.

Comments (single-line)

Your computer is very straightforward. It executes each line of code from top to bottom, and if it encounters anything weird, it throws an error. However, there’s a very convenient way of adding non-structured (random) text to your JavaScript programs. It’s called comments. To add a comment, you should place two slashes at the beginning of the string.

This will exclude this string from being executed by Node.js

// this is my first comment
console.log('hello world!')

With comments, you can add extra information to your programs that could be helpful to the “future you” or other people that will be reading your code at some point.

So, remember, if you place two slashes at the very beginning of your JavaScript file, this line will be ignored.

In this example nothing is printed to the screen as the only console.log command that I have has been commented out.

// this is my first comment
// console.log('hello world!')

Multi-line comments

You can also add multi-line comments like this

/* a
* quick
* brown
* fox
* jumped
* over
* a big elephant
  */
console.log('hello')

The comment should start with the slash-star combo and end with the star-slash. Everything between those two combos will be ignored.

With multi-line comments, you can comment out multiple lines of code!

  console.log('hello')
  /*console.log('hello')
  console.log('hello')
  console.log('hello')*/
  console.log('hello')

That’s it for now. In the next lesson, you’ll learn how to create variables and constants in JavaScript and what’s the difference between them.