简体   繁体   中英

JavaScript If / else statement not return false statement

Function returns true, however if / else statement is logging false result. Any idea where I'm going wrong?

 function loginDetails(arrayCheck, value) { for(i = 0; i < arrayCheck.length; i++){ if(arrayCheck[i] === value){ return true; } } return false; } var username = [1,2,3,4,5,6,7,8,9,10]; document.write('Login Details: ', loginDetails(username, 9), '</p>'); if(loginDetails === true) { document.write('Redirect ....Welcome !!</p>'); } else { document.write('There seems to be an error please try again !!'); }

loginDetails is a function. You then test to see if it is boolean true . Funnily enough, it never will be!

I presume you actually want to run the function. You will need to cache the result in order not to run it twice:

 function loginDetails(arrayCheck, value) { for(i = 0; i < arrayCheck.length; i++){ if(arrayCheck[i] === value){ return true; } } return false; } var username = [1,2,3,4,5,6,7,8,9,10]; var loggedIn = loginDetails(username, 9); document.write('Login Details: ', loggedIn, '</p>'); if(loggedIn === true) { document.write('Redirect ....Welcome !!</p>'); } else { document.write('There seems to be an error please try again !!'); }

What do you mean by if(loginDetails === true) ? This doesn't pass any parameters to loginDetails function.
Instead try if(loginDetails(username, 9) === true) . Hope this works.
Else store loginDetails(username, 9) in a variable and check whether that variable is true ?

loginDetails is a function I suppose you want to check its result to be equal with true.

 function loginDetails(arrayCheck, value) { for(i = 0; i < arrayCheck.length; i++){ if(arrayCheck[i] === value){ return true; } } return false; } var username = [1,2,3,4,5,6,7,8,9,10]; var loginDetailsResult = loginDetails(username, 9); document.write('Login Details: ',loginDetailsResult, '</p>'); if(loginDetailsResult === true) { document.write('Redirect ....Welcome !!</p>'); } else { document.write('There seems to be an error please try again !!'); }

You are checking if the reference to the function is equal to true, which will always evaluate to false. A function and a boolean are different types and therefore comparing for strict equality will always return false. I have corrected the code, so that the function is called, and the result of the function is compared, instead of the reference to the function.

 function loginDetails(arrayCheck, value) { for(i = 0; i < arrayCheck.length; i++){ if(arrayCheck[i] === value){ return true; } } return false; } var username = [1,2,3,4,5,6,7,8,9,10]; document.write('Login Details: ', loginDetails(username, 9), '</p>'); if(loginDetails(username, 9) === true) { document.write('Redirect ....Welcome !!</p>'); } else { document.write('There seems to be an error please try again !!'); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM