1

Is there a better way to check for multiple variables in an if statement JavaScript?

num2 = 23;
num1 = 3;

if(typeof num1 === 'number' && typeof num2 === 'number'){
    
}
1
  • What exactly are you doing you need to check the types, might be better solution.... Commented Sep 22, 2020 at 3:24

1 Answer 1

6

You might make a function that checks if every one of its arguments is of the desired type:

const isType = (type, ...args) => args.every(arg => typeof arg === type);
if (isType('number', num1, num2)) {

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

Comments

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.