user3364498
user3364498

Reputation: 489

Any real difference between window.prompt and prompt?

Just out of curiosity is there any difference between window.prompt and prompt in JavaScript.

For one of the answers to my exam questions the teacher uses

var yourName = window.prompt("Please enter your first name here\n");

and I used

var yourName = prompt("Please enter your first name here\n");

It is a written exam so i felt it wouldn't make much difference.

Upvotes: 4

Views: 4394

Answers (2)

Muhammad wasi
Muhammad wasi

Reputation: 11

var person = prompt("Please enter your name", "Harry Potter");

if (person != null) {

document.getElementById("demo").innerHTML =

"Hello " + person + "! How are you today?"; }

usually window.prompt === prompt because,

A prompt dialog contains a single-line textbox, a Cancel button, and an OK button, and returns the (possibly empty) text the user entered into that textbox. ... alert Dialog boxes are modal windows; they prevent the user from accessing the rest of the program's interface until the dialog box is closed

Upvotes: 0

Bergi
Bergi

Reputation: 664548

Usually yes, window.prompt === prompt. Yet it does depend on your scope, someone might have declared window or prompt variables with different values than those in the global scope.

For further details have a look at Is window really global in Javascript?. You (and your teacher) also might be interested in Why is it beneficial to rely on the scope chain alone and avoid explicitly referencing the head object in Javascript?.

Upvotes: 6

Related Questions