# JS Tip of the Day: Not All Functions Are Constructors

**URL:** <https://forum.kirupa.com/t/js-tip-of-the-day-not-all-functions-are-constructors/643140>\
**Category:** web dev\
**Created:** [March 27, 2020, 12:55pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-not-all-functions-are-constructors/643140 "2020-03-27T12:55:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [March 27, 2020, 12:55pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-not-all-functions-are-constructors/643140/1 "2020-03-27T12:55:22Z")

</div>

**Not All Functions Are Constructors**  
Version: ES2015+ (new function types)  
Level: Intermediate

It used to be that whenever you made a function, that function could also be used as a constructor to make new object instances. This sometimes also made it confusing to know which functions were meant to be used simply as functions and which were to be used as constructors. The convention of always capitalizing the first letter in a constructor function’s name was used to helped make this distinction.

```javascript
function MyConstructor () {
    // ...
}

function myNormalFunction () {
    // ...
}

```

With ES2015, the new `class` syntax came along to help with this problem. Not only can you tell from `class` that what’s being defined is to be used only to create new object instances, but if you try to call a `class` constructor as a normal function, you’ll get an error.

```javascript
class MyClass {
    // ...
}

new MyClass(); // Ok
MyClass(); // TypeError: cannot be invoked without 'new'

```

Flipping that around, all other new function types added in ES2015 and beyond can only be used as functions and not constructors. They will cause an error if you try to use them with `new`.

```javascript
// arrow functions
let arrow = () => {};
new arrow(); // TypeError: not a constructor

// methods (object and class)
let obj = {
    method () {}
};
new obj.method(); // TypeError: not a constructor

// generator functions
function * gen () {}
new gen(); // TypeError: not a constructor

// async functions
async function nsync () {}
new nsync(); // TypeError: not a constructor

```

The only kinds of functions that can be constructors are:

- Normal `function` declarations and expressions
- `class` constructor functions
- Functions created with the `Function` constructor

More Info:

- [function on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)
- [class on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/class)
- [new on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new)
- [Arrow functions on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)
- [Method definitions on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions)
- [function\* on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*)
- [async functions on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)
- [Function on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function)
