Console input in JavaScript

Hisham Thavarakkadan
3 min readMar 30, 2021
Photo by Florian Olivo on Unsplash

As my journey continues as a student of JavaScript at The Hacking School, I wanted to take user input from command line to make simple programs to test my skills. That is when it struck me that there is no direct way to take user input in JavaScript.

As a language which is built for the web, command line input is not a built-in functionality of the language.

As I researched further I found that NodeJS has built-in module readline which may solve my issue, but on further reading , I found that it requires extensive understanding of asynchronous behavior of JavaScript. The session on asynchronous behavior of JavaScript were to start in a week, but I still wanted to take command line input. So I was wondering, Is there any simple way to solve my problem,

Yes there is!!

readlineSync

I am gonna introduce you a npm package readlineSync, It implemented all the functionality we need to take input form the user on a command line.

You can install it in your working folder by

Now that you installed readlineSync let’s see some simple example using the same

Let’s ask user name and say hello.

In order to readlineSync do the magic we need to import it first

Now let’s ask user to input.

In the console

Now let’s look when you want to enter secret information

This will result in

There are many functionality for readlineSync do checkout their doc.

To make simple command line application using JavaScript and node readlineSync is the best tool at your disposal, without worrying about complex JavaScript functionality you can more focus on your logic.

Hope this gives a quick intro towards readlineSync

Happy coding :)

--

--