0

I created a function called add.

function add(a,b){
  return a + b;
}

add.val = 10;
console.log(add) // => function add()
console.log(add.val) // => 10

I’d like to know how and why it is possible to create the val property on add. Why do we only see the output as function when add also has val on it?

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
  • 1
    Duplicate of [A function and an object at the same time?](https://stackoverflow.com/q/33097986/4642212). – Sebastian Simon Apr 11 '21 at 17:26
  • 1
    Everything in js are objects, even a function. You just adding a new property `val` to the object `add` – Mister Jojo Apr 11 '21 at 17:27
  • 1
    _“And why we only see output as function when add also has val on it.”_ — What do you mean? `console.log(add)` shows something like _function add()_ just fine without the additional property. – Sebastian Simon Apr 11 '21 at 17:28
  • And speaking of _“internals”_ — Internally, a function is just an object with a _\[\[Call\]\]_ internal slot. If you want to learn more about internals, read the [specification](https://tc39.es/ecma262/#function-object). – Sebastian Simon Apr 11 '21 at 17:39
  • Related: [How is almost everything in Javascript an object?](https://stackoverflow.com/q/9108925/4642212). – Sebastian Simon Apr 11 '21 at 17:44

0 Answers0