How to check if a function is defined in JavaScript

1 Answer

0 votes
if (typeof sum === 'function') {
  	console.log('sum function is defined');
} else {
  	console.log('sum function is not defined');
}

function toBinArray(arr) {
 
}

if (typeof toBinArray === 'function') {
  	console.log('toBinArray function is defined');
} else {
  	console.log('toBinArray function is not defined');
}
  
 
 
 
   
/*
run:
  
"sum function is not defined"
"toBinArray function is defined"
   
*/

 



answered May 27, 2022 by avibootz
...