So I’m trying to call where if not undefined and index is foo so I use:
if (typeof(getdata(js, 'box1')) != "undefined"
&& (getdata(js, 'box1')).indexOf('foo') >= 0) {
// Do something
}
This works fine but I dont want to call the getdata twice. Is there way to say if getdata is not undefined and the indexOf is foo then do something, without calling the getdata() function twice?
Alternative:
While this allows you to get away with a single check, involving a regular expression for such a simple test could be frowned upon 🙂
You’re better off using a local variable for storing the outcome of the function call:
Also note that
typeofis an operator and not a function: