How to Get User Input in JavaScript | Explained

How to Get User Input in JavaScript

Learn how to get user input in javascript easily with example code. One of the great things about programs is that we can allow users to interact with them. In javascript, there are a lot of different ways that we can give our programs information. We could use things like text boxes or buttons. But in this article, I am going to show you the most basic way that you can do it in Javascript. And that’s by using something called a “prompt()”. It is used to get user input and we will use that input in order to do certain things in our program.

Example:

var name = window.prompt("What is your name?");
document.write(name);

Output:

how to get user input in javascript

  1. Here, we have created a variable and I have given a variable name as “name”.
  2. Actually set this variable equal to a special value as “window.prompt”. And basically, this is going to prompt the user to give us specific information. Whenever information the users give us is going to store inside of variable “name”.
  3. Inside the prompt parentheses, we can actually type a message for the user. So, I have given “what is your name” inside the parentheses. Basically have a prompt box that prompts the user to give us their name.
  4. Then we printed the “name” using document.write().

Leave a Reply