Wednesday, July 3, 2024
HomeLanguagesJavascriptJavaScript is showing reference error “Prompt is not defined”

JavaScript is showing reference error “Prompt is not defined”

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:

 

 

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments