# const() function syntax

**URL:** <https://es.discourse.group/t/const-function-syntax/1456>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [September 10, 2022, 2:20am UTC](https://es.discourse.group/t/const-function-syntax/1456 "2022-09-10T02:20:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![taloskhaos](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/taloskhaos/32/1497_2.png) [@taloskhaos](https://es.discourse.group/u/taloskhaos)\
**Post date:** [September 10, 2022, 2:20am UTC](https://es.discourse.group/t/const-function-syntax/1456/1 "2022-09-10T02:20:05Z")

</div>

This probably seems like a quaint request but const functions are seeing a lot of use for their immutability, non-hoisting, and other benefits.

Why have we not seen syntax such as:

```javascript
   const doSomething() {

   }

   // equivalent to:

   const doSomething = () => {

   }

```

I am not suggesting to replace the arrow syntax, simply add a shortcut that is a little more human readable to people coming from other languages. I hear a lot of people who refuse to use the const syntax despite having decent usecases for it because of the unnecessary arrow which does cost three-keystrokes plus the extra spacing with the arrow.

---

<div class="post-metadata">

**Author:** ![tabatkins](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/tabatkins/32/170_2.png) [@tabatkins](https://es.discourse.group/u/tabatkins)\
**Post date:** [September 10, 2022, 3:33pm UTC](https://es.discourse.group/t/const-function-syntax/1456/2 "2022-09-10T15:33:31Z")

</div>

A "const function" in other languages usually means something substantially different - usually it indicates an idempotent function which is guaranteed to depend only on its arguments and no other state. It would probably actually be _confusing_ to people from other languages if all it meant in JS was "you can't reassign this variable".

Plus it would mean we could never use this syntax space for anything else, such as an actual idempotent function. It would be a shame to do so when the benefit is just three characters of savings.

---

<div class="post-metadata">

**Author:** ![Josh-Cena](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/josh-cena/32/1408_2.png) [@Josh-Cena](https://es.discourse.group/u/Josh-Cena)\
**Post date:** [September 11, 2022, 3:38am UTC](https://es.discourse.group/t/const-function-syntax/1456/3 "2022-09-11T03:38:44Z")

</div>

> [@taloskhaos](#):
>
> non-hoisting

Why is non-hoisting a benefit, though? People don't usually re-assign their declarations, so hoisting means you can safely access your function declaration from anywhere. I really like writing code like this:

```js
function doSomething() {
  doA();
  doB();
  doC();

  // technical details
  function doA() {
    // ...
  }
  function doB() {
    // ...
  }
  function doC() {
    // ...
  }
}

```

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [September 11, 2022, 4:06am UTC](https://es.discourse.group/t/const-function-syntax/1456/4 "2022-09-11T04:06:45Z")

</div>

Relying on hoisting is considered (not by everyone, of course, but by many) to be poor style, and the Airbnb JavaScript style guide forbids doing so.

This is of course highly subjective, but i find code far more readable when I’ve read the import/require/definition of something before i encounter any uses.

---

<div class="post-metadata">

**Author:** ![Josh-Cena](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/josh-cena/32/1408_2.png) [@Josh-Cena](https://es.discourse.group/u/Josh-Cena)\
**Post date:** [September 11, 2022, 5:15am UTC](https://es.discourse.group/t/const-function-syntax/1456/5 "2022-09-11T05:15:11Z")

</div>

It's also subjective, but I like to start with a conceptual understanding of "what it does" before going into "how it does that", rather than getter immersed by all the implementation details upfront. I don't have strong opinions either way, but I don't think a gimped construct is necessarily a better construct—you can always use linters to restrict the freer form to what you want, but you can't do it the other way round.

(That's more about me ranting about the design pattern JavaScript is moving towards—for example [Iterator.from should guarantee a branded object, and helpers should brand-check the receiver · Issue #224 · tc39/proposal-iterator-helpers · GitHub](https://github.com/tc39/proposal-iterator-helpers/issues/224) ^^)

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [September 11, 2022, 3:09pm UTC](https://es.discourse.group/t/const-function-syntax/1456/6 "2022-09-11T15:09:17Z")

</div>

Forcing a burden onto linters - which everyone should use but many people unfortunately do not - is always a worse choice than the language being proscriptive, imo.
