JavaScript is showing reference error "Prompt is not defined"
Last Updated :
02 Jun, 2023
In this article, we cover the reference error that is "Prompt is not defined". It's important to understand the tools you are using. I know, this can be very overwhelming in the beginning. but here, we will solve this reference error with not only one solution but also make more ways to done properly.
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.
However, prompt and window are not defined in the node environment. whenever we try to run this type of program in that type of environment, that time generates this reference type error.
One thing keeps in mind prompt is defined on the client side. That is why it is not defined on server side.
Error: When we run the below code on the server side, it'll give an error like the below:
JavaScript
let name = prompt("What's your name");
console.log("hello" +name+ "!");
Output:
Here, the node command prompts an error.
Solution 1: The most effective solution is we have to install "prompt-sync". make sure you have also installed an updated version of npm and node, then write the below code in the terminal:
npm install prompt-sync
Example: This example will demonstrate the use of prompt on the server side by using the "prompt-sync" package:
JavaScript
const prompt=require("prompt-sync")({sigint:true});
let name = prompt("What's your name");
console.log("hello"+name+"!");
Output:
Error is gone by the 'prompt-sync' module
Note: If you run this type of code in nodejs environment. but, prompt is not defined in this type of environment.
Solution 2: Let's simplify this problem in another way. All computers have browsers or search engines. run this type of code in the browser terminal. let's see, it is running butter smoothly in the browser without installing any npm libraries.
Example 1: Here, we demand the user name by the prompt box. then it will simply print with the username which is given in the prompt dialog box.
Path: Open chrome >> more tools >> developer tools (ctrl+shift+i).
Terminal solution with simply print message.
Example 2: Here, we required input of the favorite language of the user. if javascript is your favorite language, it simply gives an alert message "it's great". otherwise, an alert is given to the user's favorite language.
JavaScript
let lang = prompt('What is your favorite programming language?');
let feedback = lang.toLowerCase() === 'javascript' ? `It's great!` :
`It's ${lang}`;
alert(feedback);
Output:
Â
Â
Similar Reads
JavaScript Error Object Complete Reference Error objects are arising at runtime errors. The error object also uses as the base object for the exceptions defined by the user. The complete list of JavaScript Error Object properties are listed below: Error types JavaScript RangeError â Invalid dateJavaScript RangeError â Repeat count must be no
3 min read
How to get rid of JavaScript error prompt ? Javascript is the most popular programming language that comes installed on every modern browser like Google Chrome, Mozilla Firefox, Internet Explorer, Microsoft Edge, etc. Browsers respond differently to JavaScript depending upon its settings or user settings.In this article, we will learn about h
3 min read
JavaScript Program to Print an Integer Entered by user Creating a JavaScript program to accept an integer from the user and then print it is a fundamental exercise that helps in understanding user input and output operations. In this article, we'll explore different approaches to achieve this task in a JavaScript environment. Approach 1: Using Prompt an
2 min read
Javascript Window prompt() Method The prompt() method in JavaScript displays a dialog box that prompts the user for input. The prompt() method returns the input value when the user clicks "OK" else returns null.Syntax:prompt(message, default);message is a string of text to display to the user. It can be omitted if there is nothing t
2 min read
JavaScript SyntaxError â Unexpected end of input A SyntaxError: Unexpected end of input error in JavaScript occurs when the interpreter reaches the end of script it is reading and it indicates that the code is incomplete, this error will prevent the code from running and mostly happens when a closing bracket, quote or parenthesis are missing, here
3 min read
Fix "java.lang.NullPointerException" in Android Studio Hey Geeks, today we will see what NullPointerException means and how we can fix it in Android Studio. To understand NullPointerException, we have to understand the meaning of Null. What is null? "null" is a very familiar keyword among all the programmers out there. It is basically a Literal for Refe
4 min read
How to take user input in JavaScript? Interacting with users is the best way to create engaging and dynamic web applications. Taking user input allows your applications to be interactive and responsive. Here we will see the approach to take user input in JavaScript, specifically using the prompt method, which is perfect for beginners.Ap
3 min read
JavaScript Course Prompt Example Now we know how we can interact with the user through the prompt, alert and confirm. Letâs build a simple application written purely using plain Javascript where we ask the user about his/her name and age, and then make use of the operators and the conditional statements we have learned till now to
3 min read
Difference between alert box and prompt box in JavaScript 1. Alert box : An alert box is one type of popup boxes in JavaScript which is often used to make sure that information have come through the user. So, the user will have to click "OK" to proceed when an alert box pops up on the window. Syntax : window.alert("message"); Another syntax to use alert bo
2 min read