Taking Input in JavaScript
•JavaScript•1 min read

JavaScriptDOM ManipulationWeb DevelopmentUser Input
Learn how to make your JavaScript programs interactive by taking user input using the prompt() function.
Taking Input in JavaScript
In JavaScript, input techniques are used to take user input, making the program interactive and dynamic.
1. prompt function:
- The
promptfunction is a built-in JavaScript function that displays a dialog box with a message to the user and an input field where the user can enter data. - The
promptfunction takes two optional arguments: a message or question, and a value pre-filled in the input field.
Example:
- Create a button element in HTML:
<button onclick="showMessage()">Click Me</button>
<h1 id="greeting"></h1>- Add a function
showMessagein JavaScript:
function showMessage() {
// Ask for user's name and store it in the variable 'name'
let name = prompt("Enter your name:");
// Change the inner text of the heading element
document.getElementById('greeting').innerText = `Hey ${name}, welcome to the website.`;
}- When the user clicks the button, it will prompt them to enter their name.
- For example, if they enter "coders", it will display:



