Natesan
Natesan

Reputation: 1145

alert is not part of Javascript?

I read about general Introduction to Object Oriented JavaScript from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript. They mentioned alert is not part of the javascript itself.

Is that true? Then how does it work?

Upvotes: 0

Views: 334

Answers (3)

user25784520
user25784520

Reputation: 21

Various JS environments (like browser JS engines, Node.js, etc.) add APIs into the global scope of your JS programs that give you environment-specific capabilities, like being able to pop an alert-style box in the user's browser.

In fact, a wide range of JS-looking APIs, like fetch(..), getCurrentLocation(..), and getUserMedia(..), are all web APIs that look like JS. In Node.js, we can access hundreds of API methods from various built-in modules, like fs.write(..).

Another common example is console.log(..) (and all the other console.* methods!). These are not specified in JS, but because of their universal utility are defined by pretty much every JS environment, according to a roughly agreed consensus.

So alert(..) and console.log(..) are not defined by JS. But they look like JS. They are functions and object methods and they obey JS syntax rules. The behaviors behind them are controlled by the environment running the JS engine, but on the surface they definitely have to abide by JS to be able to play in the JS playground.

You-Dont-Know-JS - Chapter 1

Upvotes: 2

jfriend00
jfriend00

Reputation: 707456

There are several parts to the programmability of a browser:

  1. The javascript language itself which has nothing specifically to do with a browser - it's a pure language. This is described by the ECMA specification and includes only the pure language and objects that are part of the language.

  2. The DOM and it's programming access (such as the properties and methods of the various HTML elements).

  3. The various host objects such as window, window.location or window.navigator and the properties and methods that they offer. This is where alert() is added.

So, alert() is not part of the actual javascript language itself. For example, when you use javascript server-side, there is no alert() method.

Upvotes: 5

Dipesh Parmar
Dipesh Parmar

Reputation: 27364

alert is part of operating system and alert() is javascript function which call the window's alert dialog box.

https://developer.mozilla.org/en-US/docs/Web/API/window.alert?redirectlocale=en-US&redirectslug=DOM%2Fwindow.alert

Upvotes: 0

Related Questions