How to call a function in an if statement in JS - javascript

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.

Related

how to conditionally include or exclude statements from function expression

i have 2 different javascript environments and want to create a library usable on both. to do that i want to use higher order functions to generate my functions, like so
try {
Maptool.chat.broadcast("")
var myFunc1 = genFunction1("env1");
}
catch(e){
if(e instanceof ReferenceError){
var myFunc1 = genFunction1("env2")
}
}
genFunction1 would return a function that i could call built from a function expression. there are only 2 ways i know to generate the function that is returned, it could be written similarly to the next code block with the if outside the inner function which would create a lot of code duplication as i would need to write everything in the function that is unchanged again.
function genFunction1(env){
if(env == "env1"){
return function () {return 3;}
}
else{
return function () {return 2;}
}
}
or with the if inside like the following code block
genFunction1(env){
return function (){
if(env=="env1"){
return 3;
}
return 2;
}
}
however if we use a object with a get property which logs when accessed we can see that the third code block calls a if for each call of myFunc1.
function genFunc1(obj){
return function (){
if(obj.env == "env2"){
console.log("3");
}
console.log("2");
}
}
const obj = {
get env() {
console.log("accessed property");
return "env2";
}
}
var myFunc1 = genFunc1(obj);
myFunc1();
myFunc1();
as previously said the earlier code block calls the get method once for each call of myFunc1(),but the environment won't change aside from the first time i check, is there any way to make the generated function not include a if and include the if only in the higher order function without duplicating code?
This is expected behavior as you are getting property from an object. Take a snapshot of the value before generating the function, like this.
function genFunc1(obj) {
var env = obj.env
return function() {
if (env == "env2") {
console.log("3");
}
console.log("2");
}
}

Javascript Use a returned value for a boolean comparison [duplicate]

This question already has answers here:
Calling a function without parentheses returns whole function as a string
(6 answers)
Closed 5 years ago.
If I have:
function Function1(){
var Value = true;
return Value;
};
How can I use the returned value "Value" in another function for it to be used as true, if I use it as below, it doesn't returns nothing.
function Function2(){
if(Function1 == true){
console.log("Hello")
}
}
function Function2(){
if(Function1() == true){
console.log("Hello")
}
}
only replace Function2 with Function2()
In order to get the returned value you need to call the function first. What you're doing is evaluating the function itself, not the returned value.
You also don't need the == true part because it will evaluate the returned value anyway.
Change your code to:
function Function2(){
if(Function1()){
console.log("Hello")
}
}
Function2 needs to be like this:
function Function2(){
if(Function1()){
console.log("Hello");
}
}
It is because you are calling a function and not a variable.
So since you are calling Function1 you need to make sure that you are calling it like this Function1().
If you are calling a variable you can just use the name.
And also since it is a variable of type boolean you can simply use if(Function1()) and omit the == true.
And you call it like this:
var k = Function1();
Maybe you have to type brackets '()' after the function:
function Function2(){
if(Function1() == true){
console.log("Hello")
}
}
Because now Syntax Parser thinks that Function1 is a variable.

How to return from a Javascript funtion without calling return?

I'm trying to simplify a line of code I do use everytime:
if (we_need_to_exit) { op(); return; }
Do I have a chance to define a function, something like this:
function my_return (x) {
x.op();
x.return;
}
and use it like:
if (we_need_to_exit) { my_return(this); }
Is it possible to define such a function?
Edit
Best simple solution that fits in my case is the following:
if (we_need_to_exit) { return op(); }
no, once you call my_return, that return inside of my_return is to return from within my_return.
You can probably do what you want by:
if (we_need_to_exit) { return my_fn(this); }
and your my_fn will be:
function my_fn (x) {
x.op();
// return any value you want, or use "return;" or just omit it
}
Why this won't work:
function my_return (x) {
x.op();
x.return; //[1] [2]
}
//-----and use it like-------------------
if (we_need_to_exit) { my_return(this); //[3]}
It means creating a return property on x, it won't return.
replacing x.return; with return; will return from the function, not from outside of it's call (at [3], which is your objective).
'my_return(this)' "this" will be set to the object calling the function the "if statement" is in.
function x(){
console.log(this); //shows the global object (window, if run in a browser)
}
var y = {
fruit:"apple",
x:function(){
console.log(this); // shows the calling object
}
};
x();
y.x();

Check if function returns true to execute another function

I have written a form validation using JS which ends with return(true);
function check() {
....validation code
return(true);
}
All I want is, need to check if check() function returns true, I want to execute another function.
Code I have tried is as follows:
if(check() === true) {
function() {
//Another function code
}
}
You should use return true; and your if statement doesn't need the === true comparison.
function check() {
//validation code
return true;
}
if(check()) {
//Another function code
}
JSFIDDLE
First of all, return is not a function, you can just do this:
return true;
Now, to only execute myFunction if check returns true, you can do this:
check() && myFunction()
This is shorthand for:
if(check()){
myFunction();
}
You don't need to compare the return value of check with true. It's already an boolean.
Now, instead of myFunction(), you can have any JavaScript code in that if statement. If you actually want to use, for example, myFunction, you have to make sure you've defined it somewhere, first:
function myFunction() {
// Do stuff...
}
You just need to modify your first code snippet. return is a keyword, what you are trying to do is to execute it as a function.
function check() {
....validation code
return true;
}
You'll need to change your 2nd snippet slightly, to execute the function too however... The simplest way is to wrap it as an anonymous function using curly braces:
if(check()) {
(function() {
//Another function code
})();
}
You're not calling the function in your affirmative clause, only declaring it. To call an anonymous function do this:
(function (){...})()
You could type
$this.myFunction=function(){
//code here
}
and to execute some code if a the myFunction function is true, you could use booleans
such as e.g.
if(//your function is true){
and so on

Is this the right way to call a function in javascript?

This is a part of my document:
function MyFunction() {
var x=""
if (x=1) {
OnBtnPbDemo_SwitchChn1(1); //This is a function
} else {
OnBtnPbDemo_SwitchChn1(0); //This is another function
}
}
I want to know if this is the right way to call the functions inside the condition.
Thank you very much.
Not entirely sure what you mean about the "right" way of calling, but you can call functions wherever as long as they're available in the scope.
You can actually shorten what you've written to this too:
function MyFunction () {
var x = "";
OnBtnPbDemo_SwitchChn1(x === 1 ? 1 : 0);
}
Unless you're actually changing the x variable inside your function however, it'll never run with 1 as the param.
Yes, calling functions is the same no matter where you are calling it.
You need to use == in if condition instead use of =
if (x==1) {
instead of
if (x=1) {
if you are going to call same function for the different x value, Try this
function MyFunction() {
var x = 1;
OnBtnPbDemo_SwitchChn1(x); //you can pass the x value directly to that function.
}
if you are going to call different function for the different x value, Try this
function MyFunction() {
var x="";
if (x==1) {
OnBtnPbDemo_SwitchChn1(1); //This is a function
} else {
OnBtnPbDemo_SwitchChn1_another(0); //This is another function
}
}
You are calling same function twice, Instead just call the function once with the value 1/0 in it.
function MyFunction() {
//Check and find value of x
if(x=="somevalue") //true condition
{
x=1;
}
else{
x=0;
}
OnBtnPbDemo_SwitchChn1(x);
}

Categories

Resources