-2

I want to do something like this: Say I already have a function named function()

If (function() == 2) {
    console.log("you are great")
}

How do I do this the correct way?

4
  • is your function named function?
    – MatejMecka
    Aug 5, 2019 at 10:40
  • 3
    Name the function and call it if(nameOfYourFunction() === 2). Aug 5, 2019 at 10:40
  • function is a reserved keyword in js for creating a function, don't use it as your function name Aug 5, 2019 at 10:43
  • Suppose you have function getCount() { return 2 }. then you can do if(getCount()==2){console.log('you are great')}. Aug 5, 2019 at 10:44

3 Answers 3

3

You can't have function named function in JavaScript - it is reserved word. Fix to your problem would be give function a specific name when declaring it.

function test() {
  // just example
  return 2;
}

Then you can use it in if condition:

if (test() === 2) {
  // do something
}
1

As mentioned in comments you have to name your function correctly (function is a reserved word in JS) and then check your condition in if statement. Here you can find simple example:

function getA() {
  return 'a';
}

if(getA() === 'a'){
  console.log('Success');
}

0

You can make global variable, call it with the function and return the value and call it inside the if condition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.