To ask a user for a value in JavaScript, either call prompt("Question") for a quick popup answer, or read document.querySelector("#inputId").value from a field on your page. Use prompt() for tiny tests and page inputs for anything real.
Time 5 min · Steps 6 · Level Easy
The fastest way to ask a user for a value in JavaScript is prompt(); the right way for a real page is reading the .value property of an input field. Prompt works when you are testing an idea in the browser console or a class exercise, because it pauses the page and hands you a string immediately. For anything with a form, a button or repeated use, read the input's value instead, because prompt blocks the page and cannot be styled.
Before You Ask for a Value in JavaScript
Decide where the answer comes from, because the two routes differ completely. Check these first:
- You have a way to run JavaScript: the browser console (usually opened from the browser menu under More Tools or Developer Tools, though labels differ slightly by version), or an HTML file with a
<script>tag. - You know which kind of ask it is: a one-off popup (prompt) or a field on the page (input, textarea, select).
- You remember that prompt and input values always arrive as strings, so numbers need converting before maths.
Steps to Ask for a Value with prompt()
- Open your JavaScript context. Open the browser console, or add
<script></script>before the closing body tag of a small HTML file. - Call prompt with a question. Write
let name = prompt("What is your name?");— the text in quotes is the message shown in the popup. - Handle the cancel case. If the user presses Cancel, prompt returns
null, not an empty string. Guard it:if (name === null) { return; }or check with?? "". - Convert to a number when needed. Use
let age = Number(prompt("Your age?"));and then checkNumber.isNaN(age)before doing arithmetic. - Trim the answer. Apply
.trim()on the returned string so accidental spaces do not count as input:name.trim(). - Use the value. Print it with
console.log()or place it on the page withelement.textContent = name.
Reading a Value from an Input Field Instead
When the value should come from a field already on the page, read the .value property at the moment the user acts, not when the page loads. Put a field and a button in the HTML, give the field an id such as userAge, then in the script select it and read its value inside the button's click handler. The pattern is: document.querySelector("#userAge").value. For a dropdown (<select>) the same .value gives the chosen option's value; for a checkbox use .checked, which returns true or false instead of a string.
A Worked Example: Age in Years to Months
Here is a complete small page that asks for an age and shows the months. The HTML holds an input with id ageBox, a button with id goBtn, and an empty paragraph with id result. The script attaches one click listener:
const box = document.querySelector("#ageBox");
document.querySelector("#goBtn").addEventListener("click", () => {
const years = Number(box.value);
if (Number.isNaN(years) || years < 0) {
document.querySelector("#result").textContent = "Enter a valid age.";
return;
}
document.querySelector("#result").textContent =
years + " years is " + (years * 12) + " months.";
});Type 5 into the box and click: the page shows "5 years is 60 months." Type nothing and Number("") becomes 0, so the guard on negative or invalid entries still catches blank input only if you also check for the empty string first — add box.value.trim() === "" to that condition if a blank box should be rejected.
What Goes Wrong When Asking for Values
Four mistakes cover most of the trouble:
- Reading .value on page load. The script runs before the user types, so you get an empty string. Always read the value inside the event handler that fires after the user acts.
- Doing maths on strings.
"5" + 1gives "51", not 6, because prompt and input values are strings. Convert withNumber()or the unary+first. - Forgetting null from Cancel. prompt returns
nullwhen cancelled, andNumber(null)is 0, so a cancelled age question can quietly become zero. Check for null before converting. - Using innerHTML for the reply. Putting a user-typed value into
innerHTMLlets typed HTML run on your page. UsetextContentfor anything a user supplied.
Which Ask Fits Your Page?
Use prompt() when the script is a one-line exercise, a console experiment, or a throwaway demo — it needs no HTML and returns immediately. Use an input field whenever the page will be used more than once, needs validation messages, or should look like part of your design; prompt popups cannot be styled and freeze everything else on the page until answered. If the value should update live as the user types, listen for the input event on the field rather than waiting for a click.
Checking the Value You Received
Before using any answer, run it through three quick checks: is it null (cancelled), is it blank after .trim(), and does it convert cleanly with Number.isNaN() when a number is expected. Log the raw value once with console.log(typeof value, value) if something behaves oddly — seeing "string" where you expected a number explains most surprises in one line.
Official source: MDN Web Docs