Well this is what happen's when you add plus before prompt, i.e. as below,

Eg :- 1

var a = prompt("Please enter a number");
console.log(a);
typeof(a);

Now in eg (1) when you enter a number and if you check that in console, it show a number but as that number is in-between double-quote, so in JavaScript it's a string, that's what it will show in typeof too when you console that.

Eg :- 2

var a = +prompt("Please enter a number");
console.log(a);
typeof(a);

Now when you console the var a and typeof a of eg(2) the result differs as we have added + before prompt. So this time we get our prompt input value as number and not string. Try you will understand what I'm saying.

Answer from frnt on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Window › prompt
Window: prompt() method - Web APIs | MDN
window.prompt() instructs the browser to display a dialog with an optional message prompting the user to input some text, and to wait until the user either submits the text or cancels the dialog.
🌐
W3Schools
w3schools.com › jsref › met_win_prompt.asp
Window prompt() Method
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST
Discussions

prompt function in JS ? (Eloquent JavaScript book)
prompt only works in browser. If you're trying to run this in node or such, it won't be found More on reddit.com
🌐 r/learnjavascript
10
1
May 14, 2021
How to store user input using prompt() in JavaScript? - Stack Overflow
If I were to have a prompt in my code like this: prompt("What is your favorite animal?"); Is there a way to store the answer? More on stackoverflow.com
🌐 stackoverflow.com
user interface - JavaScript's prompt, confirm and alert considered "old-fashioned" - Software Engineering Stack Exchange
Lately I've been developing a web-based management system for a gym. Their previous app was developed in Visual Basic. For the new app, all the front-end scripting uses jQuery, the server is runnin... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
September 6, 2011
Input as number in JavaScript prompt - Stack Overflow
Can we customize a prompt box in JavaScript so it takes time as input? var r = prompt("ENTER THE NUMBER OF SECONDS AFTER WHICH YOU WANT TO BE REMINDED"); More on stackoverflow.com
🌐 stackoverflow.com
🌐
Codecademy
codecademy.com › docs › javascript › window › prompt()
JavaScript | window | prompt() | Codecademy
February 20, 2025 - The prompt() function is a built-in JavaScript method that displays a modal dialogue box prompting the user for input. It is part of the Web API, provided by the browser’s window object, and pauses script execution until the user responds.
🌐
SheCodes
shecodes.io › athena › 1491-how-to-use-prompt-if-else-in-javascript
[JavaScript] - How to Use Prompt & If Else in JavaScript - | SheCodes
Learn how to create a prompt question with different spelling answers using JavaScript, using the `prompt()` function to ask the user a question, followed by the `if...else` command.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-window-prompt-method
Javascript Window prompt() Method - GeeksforGeeks
August 5, 2025 - The window.prompt() method in JavaScript is a built-in function that displays a dialog box prompting the user to input some text.
Find elsewhere
🌐
Reddit
reddit.com › r/learnjavascript › prompt function in js ? (eloquent javascript book)
r/learnjavascript on Reddit: prompt function in JS ? (Eloquent JavaScript book)
May 14, 2021 -

Hi there,

I started to teach my self JS via the book " Eloquent JavaScript"

I encounter the use of prompt function in the book, which I understand is suppose to take an input from the user (similar to 'scanf' in C (?))

I'm using Visual Studio Code. When I try to run this code:

let theNumber = Number(prompt("Pick a number"));
if (!Number.isNaN(theNumber)) {
console.log("Your number is the square root of " +
theNumber * theNumber);
}

I'm getting an error message: "ReferenceError: prompt is not defined"

How can I use to "prompt" function in JS ?

Thanks for helping!

🌐
SheCodes
shecodes.io › athena › 111739-how-to-use-prompt-in-javascript
[JavaScript] - How to use prompt in JavaScript - SheCodes | SheCodes
Learn how to use the prompt() function in JavaScript to display a dialog box and get user input
Top answer
1 of 4
56

1. UX: message boxes are mostly evil

Alert boxes are bad in all cases from the UX point of view. In desktop apps. In web apps as alerts or inline JavaScript messages. Everywhere.

You can read About Face 3 by Alan Cooper¹ if you want to know why; it explains very well how does this interrupt the workflow and annoys the user, and how nearly every alert box which exists in current software is deeply wrong. On page 542, "The dialog that cried “Wolf!”" explains that alert boxes are dismissed routinely, so their model is completely broken. On page 543 of the book are listed three major design principles:

  • Do, don't ask.
  • Make all actions reversible.
  • Provide modeless feedback to help users avoid mistakes.

Then, the authors tell us how to replace the alert boxes by the correct design approach.

Prompt messages are slightly different. And still, they break the user experience of your app. If you want the user to enter something, consider using a textbox or a textarea, decorating it with JavaScript when need. Don't be lazy, provide a rich interface in an era of RIA and AJAX-enabled apps; in all cases, if JavaScript is disabled, your prompt will not be displayed.

In web pages, both alert boxes and prompts are mostly annoying. Some examples:

  1. Some forums let you create lists by prompting infinitely for the list items. It means that while creating the list, you cannot use the page itself, including copy-paste. You also have a single tiny field. What about long text? What about bold and italics?

  2. "If you continue, the photo will be removed definitively from your profile. Are you sure?". Of course I am sure! Would I click "Remove photo from my profile" otherwise? Why your web app supposing I'm so stupid? Actually, Google applications as GMail show the correct approach. You can remove, delete, destroy whatever you want, and when you do so, the app displays a small "Undo" link.

  3. "Do you want to take our greatest survey?". Well, actually I was there to visit your website, but since you bother me with your annoying messages, I would rather go somewhere else.

  4. "The right click is disabled on this website in order to protect copyrighted photos". Well, actually I right-clicked to change the language of spell checker before sending my comment. Sure, I'll send it without checking the spelling.

Conclusion: from user experience point of view, the applications use message boxes wrongly most of the time.

But wait! Many low-quality websites replace annoying alert boxes by annoying JQuery messages with a semi-transparent background which covers all the page. So the drawbacks remain.

Well, there are another reasons not to use message boxes in web applications :

2. Design: alert boxes have their own design

You can't design an alert box at all. You can't change its color, its size, its font. This makes it even more annoying for the user: you were working with a web app, and your workflow is broken by a message which seems to come from nowhere and does not even match the visual aspect of the app. Not counting that the language of the buttons also match the OS/browser language, not the web application one.

For designers, JavaScript messages are much more powerful than the alert boxes.

They are also much more extensive. You can add bold and italic, you can choose your own buttons (what about: "We apologize but the password you entered is invalid. [Reset my password] [Try another one] [Cancel]"?)².

3. JavaScript: application flow stops

When displaying an alert box, JavaScript stops executing until the user clicks. On a website, it might be ok. With a web app, it often becomes a problem.

4. Sandbox: don't force the user to reboot his computer

Remember the crappy websites which show you an infinite number of message boxes? The only way for the users without enough technical background to be able to continue working was in fact to reboot their computer. This brings us to a problem: alert boxes are out of the scope of the website or web application. You are not authorized to prevent the user from accessing other tabs of the browser³.

The same problem forced the browsers to solve it in different ways. Firefox, for example, permits the access to other tabs when displaying an alert on your tab. Chrome, on the other hand, allows you to check that you don't want to get any alert boxes from a page any-longer, but still blocks the access to other tabs.

While Firefox approach is perfectly valid, Chrome's one can be criticized (since it still blocks every tab), and causes a problem: what if the user was severely annoyed by several message boxes issued by your app and checked the case, and then, you tried to show something really important? Right, the user will never see it.

The fact remains the same, most users will be annoyed by alert boxes, so they are still not very user friendly, and may severely block a user with no enough technical background. Inline, JavaScript messages may block the page, but not the browser itself. Since web app model is a sort of sandboxing, where you can't for example access the user keyboard or reboot the computer or read files from hard disk or go full-screen or use two monitors, alert boxes with their blocking effect break severely this sandboxing model.

Last but not least, what if the user was on another tab when your application decided to show the alert box? What if the user was doing something important, and does not want to interact with your app right now?


¹ About Face 3, The Essentials of Interaction Design, Alan Cooper, Robert Reimann and David Cronin, ISBN 978-0-470-08411-3; Chapter 25: Errors, Alerts, and Confirmations.

² This is just an example. Please, don't do it in your web applications, since it's really a poor design choice.

³ If you want a comparison with the world of desktop apps, an inline JavaScript message is like a message box of a desktop application. An alert box in a browser, on the other hand, is like a window appearing from nowhere, set as topmost, on a full-screen opaque background which blocks you from accessing any other desktop application. Any app which will decide to do it once on my computer will be removed immediately, and forever.

2 of 4
3

Bottom line: The default prompt, confirm and alert dialog boxes match the style of your browser, not the style of your site. Most UI people want all dialog boxes to flow with the UI of their site. They use modal windows to present messages and collect data using the same fonts and colors as the site's UI, not the default grey and blue of MSIE or FF.

🌐
Medium
medium.com › @meghamurari12 › lesson-16-user-input-with-prompt-in-javascript-df46a299ecab
📘 Lesson 16: User Input with prompt() in JavaScript | by Meghamurari | Medium
August 9, 2025 - The prompt() function in JavaScript displays a dialog box that prompts the user for input. It's a simple way to interact with users and…
🌐
Medium
medium.com › @dinusha-s › javascript-prompt-tutorial-from-simple-arrays-to-smart-objects-b9175d5b6821
JavaScript Prompt Tutorial: From Simple Arrays to Smart Objects | by Dinusha Somarathne | Medium
October 20, 2025 - In this article, we’ll explore two easy approaches using JavaScript: ... Both are great for beginners who want to learn how to work with data structures and the DOM. In this first approach, we’ll store the user’s details in a simple array. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Collect User Data</title> </head> <body> <h1>User Information</h1> <button onclick="showPrompt()">Show Prompt</button> <div id="output">Output will appear here</div> <script> function showPrompt() { // Create an empty array for each entry let data = []; let name = prompt("What is you
🌐
npm
npmjs.com › package › prompt
prompt - npm
April 11, 2022 - $ node examples/simple-prompt.js prompt: username: some-user prompt: email: some-user@some-place.org Command-line input received: username: some-user email: some-user@some-place.org
      » npm install prompt
    
Published   Apr 11, 2022
Version   1.3.0
Author   Nodejitsu Inc.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Interaction: alert, prompt, confirm
September 8, 2020 - let age = prompt('How old are you?', 100); alert(`You are ${age} years old!`); // You are 100 years old!
🌐
Stack Overflow
stackoverflow.com › questions › 47649426 › input-as-number-in-javascript-prompt
Input as number in JavaScript prompt - Stack Overflow
Can we customize a prompt box in JavaScript so it takes time as input? var r = prompt("ENTER THE NUMBER OF SECONDS AFTER WHICH YOU WANT TO BE REMINDED");
🌐
SheCodes
shecodes.io › athena › 90671-what-is-prompt-in-javascript-how-to-use-it
[JavaScript] - What is prompt in JavaScript? How to use it? | SheCodes
Learn about the prompt function in JavaScript, its usage, and how to use it to get user input and store it as a string.
🌐
Dummies
dummies.com › article › technology › programming-web-design › javascript › how-to-prompt-the-user-for-input-in-javascript-140912
How to Prompt the User for Input in JavaScript | dummies
July 3, 2025 - To store user‐entered data in a variable, you create a new variable and then follow it with =. You then follow it with the prompt statement. ... It's important to note that a single equal sign (=) in JavaScript is called the assignment operator.
🌐
OpenAI
platform.openai.com › docs › guides › prompt-engineering
Prompt engineering | OpenAI API
Any instructions provided this way will take priority over a prompt in the input parameter. ... 1 2 3 4 5 6 7 8 9 10 11 import OpenAI from "openai"; const client = new OpenAI(); const response = await client.responses.create({ model: "gpt-5", reasoning: { effort: "low" }, instructions: "Talk like a pirate.", input: "Are semicolons optional in JavaScript?", }); console.log(response.output_text);
🌐
TutorialsPoint
tutorialspoint.com › javascript-prompt-example
JavaScript Prompt Example
February 25, 2025 - In this article, we will learn to use the prompt() function in Javascript. The prompt() method in JavaScript allows developers to collect user input through a pop-up dialog box. What is the prompt() Method in JavaScript? The prompt dialog box is very