When learning a new programming language, we usually write small programs that read the information we provide, process this information, and produce the result. This way we can easily spot the mistakes in our code: if the result of our code doesn't match our expectations, we are sure that there is a bug somewhere, and we can find an error and fix it. That's why we will focus on taking user's input.
Unfortunately, there is one challenge on our way. Unlike other languages like Python or Java, JavaScript doesn't have some built-in instruments to capture the user's input from the terminal. To simplify this process and help you learn, our team has developed a library called sync-input.
In this topic, we will find out how to use it to read input.
What is sync-input?
Sync-input is a library designed to simplify taking user input from the terminal in Hyperskill JavaScript projects.
If you are solving a project or a problem on the Hyperskill website, this library is already "built" inside the code, and you can just use it with no extra work. If you decide to develop the same program on your own, please, read our article that explains the installation process.
Using sync-input is also quite simple. You can think of it as an integration into your JavaScript projects that lets you easily ask users for information in a straightforward way.
Here's what you need to do. First, ensure that your project includes sync-input. You should find this line at the beginning of your code:
const input = require('sync-input');This line essentially defines the input function that you will use to collect user inputs from the terminal in your project.
Now that you have sync-input ready, you can start requesting information. For example, to get a simple text input, you can do this:
let name = input();The line above calls the input function to obtain user input from the terminal and assigns the input value to the variable name. This process makes your program wait for the user to type something and press Enter.
Specifying input data types
If you need to specify your input type as a number, you take the input as before but then convert it into a number like this:
let age = Number(input());Be careful, though, because if the user types something that's not a number, you'll get an odd result called NaN (Not a Number).
You can also make your program more friendly by asking questions with your input request. So, when you want to print a message while taking input, you can pass it to the function as an argument:
let name = input("Enter your name: ");
let age = Number(input("Enter your age: "));
console.log(`Hello ${name}, you are ${age} years old!`);
/*
Output:
Enter your name: John
Enter your age: 26
Hello John, you are 26 years old!
*/Conclusion
Taking input from the terminal can be tricky with JavaScript, especially for CLI programs and projects on this platform. At the same time it is a common way to learn programming by reading and printing information. That's why our team created the sync-input library to simplify the process. of taking input. In this topic, we've covered how to use it in your JavaScript projects. Happy learning!