text output with
To print out a text to the screen in Node.js you should use the command
Make sure to follow the pattern that I show you.
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.
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.
- Double quotes -
- Single quotes -
- 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.
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.
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.
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!')
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')