0

I need some condition to catch and throw error when a non-function data type is passed as the second argument.

Including undefined being passed?

function test(fn) {
  console.log(fn)
  // should throw an error if a non-function data type is 
  //passed as the second argument (includes undefined being passed)
  if (fn && typeof fn !== 'function') throw Error();
  return fn;
}


function demo() {}

test(); //undefined OK!

test(undefined); // SHOULD THROW ERROR ALSO

// throws error since argument is defined and not a function
test('sdfasfs'); 

4
  • Possible duplicate of stackoverflow.com/questions/5999998/… Commented May 11, 2018 at 4:58
  • Your approach seems correct . Whats the error? Commented May 11, 2018 at 5:09
  • 1
    So you want to be able to distinguish between test() and test(undefined)? Or should those be treated the same? Commented May 11, 2018 at 5:17
  • Yes, I need to be able see the difference Commented May 11, 2018 at 5:49

3 Answers 3

1

The only way to distinguish between implicitly and explicitly passing undefined is to look at how many arguments have been passed:

function test(fn) {
  console.log(fn)
  // should throw an error if a non-function data type is 
  //passed as the second argument (includes undefined being passed)
  if (arguments.length > 0 && typeof fn !== 'function') throw Error();
  return fn;
}


function demo() {}

test(); //undefined OK!

test(undefined); // SHOULD THROW ERROR ALSO

// throws error since argument is defined and not a function
test('sdfasfs'); 

Sign up to request clarification or add additional context in comments.

Comments

1

It will not throw an error because of fn condition in if which will be evaluated to false for undefined and hence, if is not evaluated to true and error is not thrown.

If you are looking for throwing an error if undefined is passed then, you can update your code to following

function test(fn) {
  console.log(fn)
  // should throw an error if a non-function data type is 
  //passed as the second argument (includes undefined being passed)
  if ((fn || fn === undefined) && typeof fn !== 'function') throw Error();
  return fn;
}

function demo() {}

test(); //undefined OK!

test(undefined); // WILL THROW ERROR

// throws error since argument is defined and not a function
test('sdfasfs'); 

Comments

0

Because of the fact that undefined is falsy in JS, your first condition prevent you from throwing.

Just remove the first condition.

function test(fn) {
  console.log(fn)
  // should throw an error if a non-function data type is 
  //passed as the second argument (includes undefined being passed)
  if (typeof fn !== 'function') throw Error();
  return fn;
}


function demo() {}

test(); //undefined OK!

test(undefined); // SHOULD THROW ERROR ALSO

// throws error since argument is defined and not a function
test('sdfasfs');

1 Comment

Then test() will throw though. Maybe that's what the OP wants, but it's unclear to me because of the //undefined OK! comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.