-1

I know that alert() is built-in function in JavaScript.
But is it a function declaration:

function alert() { ... }

or function expression?

var alert = function() { ... }

So, are we using the function's name or the variable's name when we call it? and likewise prompt() and confirm() ..
Thanks in advance.

Moaaz Bhnas
  • 593
  • 1
  • 11
  • 29
  • 1
    I don't think JavaScript's built-in functions are written in JavaScript. Anyway, why do you need to know? – Phil Dec 06 '17 at 22:36
  • 2
    What difference does it make? It's a native function exposed by the browser; it's probably **neither** as it's native code. – Pointy Dec 06 '17 at 22:36
  • @Pointy Just wanted to understand .. – Moaaz Bhnas Dec 06 '17 at 22:37
  • Symbols that are bound are not distinguished as to *how* they were bound. If you declare a function with a function declaration statement, or with a `var` declaration, you can still use that declared symbol any way you want. (A `const` declaration is obviously special.) – Pointy Dec 06 '17 at 22:38
  • Possible duplicate of [var functionName = function() {} vs function functionName() {}](https://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname) – Aluan Haddad Dec 06 '17 at 22:40
  • 2
    Put it this way: if you got one answer or the other, what would you do differently? – Pointy Dec 06 '17 at 22:40
  • Thanks @Pointy for the explanation. One more question please _off-topic_, Is this a bad question to ask on Stack? Is this app only for code problems not to understand concepts? – Moaaz Bhnas Dec 06 '17 at 22:43
  • 2
    I don't think it's a "bad" question, but when you ask something like this it's helpful to explain why getting an answer is important. What are you working on? How will the answer help you do what you want to do? – Pointy Dec 06 '17 at 22:49
  • 1
    Thanks @Pointy, I'll consider these things the next time I ask. – Moaaz Bhnas Dec 06 '17 at 23:00
  • 1
    I enjoy your questions and I hope you are successful in learning many new things here. – Pointy Dec 06 '17 at 23:01

2 Answers2

7

But is it a function declaration or function expression?

Neither. It is a function.

Function expressions and declarations are bits of JavaScript syntax used to create functions. How alert is created is an implementation detail of the browser (and you'll probably find that it is written in C++ and exposed to the JS engine as a library).

So, are we using the function's name or the variable's name when we call it?

You use a variable (or a property, or a constant) to get a reference to the function, which you then call.

The name is mostly used by debugging tools to report on which function is being used for something.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
-1

It's a javascript predefined window.alert() method. You call the method into action when you use alert();