Skip to content

How to take input in JavaScript without prompt

  • by

Different host environments have different methods to take input in JavaScript without prompt. You’ve identified one for browsers, DOM is another. NodeJS has the Readline API (among others). There are lots of other environments (WSH, JXA, etc, etc).

JavaScript has no native means of getting input. Every method that exists is an extension provided by the host environment (prompt is an example of that).

The built-in readline Node.js module could be used to prompt the user and request input. But you can also try inquirer or prompt

To write messages to stdout, console.log() is the simplest native solution (available in both browser and Node.js contexts)

Take input in JavaScript without prompt

A simple example code gets user input.

<!DOCTYPE html>
<html>
<body>
  <script>
    var name = window.prompt("Enter your name: "); 
    alert("Your name is " + name);
  </script>
</body>
</html>

Output:

How to take input in JavaScript without prompt

Use this if you want to use confirm, which would give you the equivalent of a Boolean expression.

var answer = confirm("Yes or no?")

Do comment if you have any doubts or suggestions on this Js input topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading