Sample Video Frame

Created by Zed A. Shaw Updated 2024-02-17 04:54:36

07: Prompting Input

This exercise will teach you about both the npm tool and how to require modules. Before you can type the code in you will need to run some commands to get a module called readline-sync installed. You don't have to use this module but it really helps when asking someone simple questions.

To install readline-sync you run the following command:

$ npm install readline-sync

This may take a while, and when it's done you will see that this module is installed. Then you can test it by running node:

$ node
> const readline = require('readline-sync')
> readline.question("? ")
? lkjasdf

If you get an error, then you didn't install readline-sync correctly. It's difficult to debug these kinds of errors, but email me at or contact me on the chat for help.

The Code

Once you have readline-sync installed you can then type in this code to ask yourself some simple questions:

const readline = require('readline-sync');

let name = readline.question("What's your name? ");
let age = readline.question("What's your age? ");
let eyes = readline.question("What's your eye color? ");

console.log(`Your name is ${name}`);
console.log(`Your age is ${age}`);
console.log(`Your eyes are ${eyes}`);

You should notice that I'm using const to keep the variable for the readline module. This prevents me from possibly replacing it and breaking my code later (which happens sometimes accidentally). I'm using let for the other variables since I will probably change them.

What You Should See

If this code works as expected then you should see this:

$ node ex07.js
What's your name? Zed A. Shaw
What's your age? 44
What's your eye color? Blue
Your name is Zed A. Shaw
Your age is 44
Your your eyes are Blue

Obviously you will want to type something different from what I did, since you are a different person. If you do not like these questions, then change them. You are free to change all of this code, and you should be changing it. The only rule is to get my version working first; then change it however you want.

Previous Lesson Next Lesson

Register for Learn JavaScript the Hard Way

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.