Is there any way to get input from the console?

The question is pretty straightforward, but with a small extra:

Of course I have already googled, but if I try out e.g. readline() in a single JavaScript file within VSCode, it doesn’t work. (Or within codepen for that matter)

So is there any way to do this in a similar way to how it’s done with python and input()?

Thanks for your answers!

Edit: Alright, on this seems to work when I use prompt().
What’s the reason why this doesn’t work with VSCode for example?

You can but with Node.js and you can use process.argv() but your file should run with node.

1 Like

What do you mean by “input from the console”? To me, prompt is getting input from the user, through the browser.

I’m not sure what you mean by “What’s the reason why this doesn’t work with VSCode for example?” VSC is an editor, not a browser.

Can you describe exactly what you are wanting to happen here?

1 Like

readline() is a NodeJS module, so in order to use this, you have to have an environment in which you use Node Packages and run the code using NodeJS.
So this won’t work in browsers, but it seems like you have found prompt() but this only works on browser console, so this won’t work in NodeJS.

So you see, JS can be run in NodeJS (Server) and Browser, which both of them have different way of running and modules. So both of them have different ways of prompting questions in console.

If you want to prompt a user using NodeJS in, I recomend using a package called inquirer.js. I have used it previously and it worked pretty well.

1 Like

Well…:smiley: This is part of something I am doing as a Freelance project. The client asked for input from the console, so I guess I will have to double check with him.

The general idea, as far as I thought, was something analogous to ‘input()’ in Python.

As far as “prompt” working through the browser, yes, that is of course true, however within, if I create a repl, I am able to give input through the integrated console, whenever prompt is called. So that is what I meant by that.

Thanks for the clarification about VSCode!