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?
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
}
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');
}
You can make global variable, call it with the function and return the value and call it inside the if condition.
if(nameOfYourFunction() === 2)
.function
is a reserved keyword in js for creating a function, don't use it as your function namefunction getCount() { return 2 }
. then you can doif(getCount()==2){console.log('you are great')}
.