Any time I have included an alert in a Javascript program, I have simply called it as "alert", for example,

alert("The value of the variable is presently " + varValue + ".");

However, I also see code that calls it as window.alert. For example,

window.alert("The value of the variable is presently " + varValue + ".");

Is there any difference between calling it one way or the other?
Is one considered better style than the other?

Does calling it by window.alert avoid some overhead (i.e. - save the program the problem of resolving where the alert comes from), thereby making the program execute more quickly?

Troy III commented: a correct answer has been given to you - you need to say THANK YOU and mark your thread as "Solved" -2

Recommended Answers

All 2 Replies

alert is an object, which happens to be of type function, of the window. Anything in the window can be called directly, and in a "global" scope.

window.alert and alert are essentially the same thing.

the difference between alert("string") and window.alert("string") is:
an { implicit call to alert function } vs { explicit call to the same }.

You can also call self.alert("string"); top.alert("string"); this.alert("string"); ...but!
They are all the same.

The "alert" function of any window, is inherited through its prototype. Which means that this frame alert is not the same as that frame alert.
That would be the main and deliberate difference.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.