Skip to content

How to accept an input in the console from the user (JS)?

I tried to create a JavaScript program that outputs the binary format of an English letter on input. I had to put the value in the code. How can the value be entered in the console when the program runs?

function returnBinaryLetter(char) {
  return ((/^[a-z]$/).test(char)) ? char.charCodeAt(0).toString(2).padStart(8, '0') : 'Sorry, that is not a letter.'

// Something like:
// const input = consoleInputFunction('Enter a number.');
// console.log(returnBinaryLetter(input.toLowerCase()));

EDIT 1: This is not for a webpage. This is a JS program which I will run using Node.js. I require a solution with just JS, not with some framework (if that is even possible, mentioning just to be specific).

EDIT 2: I have made the code better after suggestions in Endothermic Dragon’s answer.



User contributions licensed under: CC BY-SA
4 People found this is helpful