+ 1

JavaScript if statements for functions

I was wondering if you could take the resulting value of a function and then use it in a if statement. Like in this case: function Test(){ confirm("Do you love dogs"); } if (Test() == true){ document.write("Awwwww that is sweet"); } else{ document.write("That is too sad"); } Any help giving is greatly appreciated.

10th Apr 2018, 7:18 PM
Bradley
2 Respuestas
+ 5
You almost have it right, but you forgot one important piece; you need to return the data from your function. CHANGE: confirm("Do you love dogs"); TO: return confirm("Do you love dogs"); ^That'll return the boolean value from the confirm() function. So if they clicked 'ok' it'll return true, otherwise it'll return false.
10th Apr 2018, 7:27 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 1
or you can do this window.onload=function(){ var result= confirm("Do you love dogs"); if (result == true){ document.write("Awwwww that is sweet"); } else{ document.write("That is too sad"); } }
10th Apr 2018, 7:37 PM
Sudarshan Rai
Sudarshan Rai - avatar