How to Check If a Javascript Function Was Called Using new Operator

Published on May 20, 2020

The property can be used to detect whether a function was invoked as a constructor using the "new" operator, or just called normally.

If the function was called using the "new" operator then refers to the constructor or function. For normal function calls will be undefined.

function myFunc() {
	if( === undefined)
		console.log('Normal function call');
		console.log('Function called using new');

// "Function called using new"
new myFunc();

// "Normal function call"
myFunc(); property can be useful when we have a function that behaves like a class, and we don't want it to be called as a normal function.

function myFunc() {
	if( === undefined)
		throw new Error('Function not called using new');

try {
catch(error) {
	// "Function not called using new"
Loading Comments