How to add if and else condition with function in JavaScript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to add if and else condition with function in JavaScript

function myFunction () { // Some code return true; } function anotherFunction() { if( myFunction() == true ) { // Some code } else { // Another code } } // Can we do this is it possible // I never get else part and myFunction() is executed it doesn't check weather the function is running or not // I need to test whether myFunction () is running or not // I hope we all going to fix it. https://code.sololearn.com/WgQvkfPNajv4/?ref=app

20th May 2020, 6:17 AM
piyush raj
piyush raj - avatar
3 Answers
+ 3
if myFunction returns true, then "if" happens, if it returns false, then "else" happens. example: function myFunction(a){ if(a % 2 == 0){ //some code return true } else{ return false }
20th May 2020, 7:38 AM
🍇 Alex Tușinean 💜
🍇 Alex Tușinean 💜 - avatar
+ 2
got it thank you @💜 Alex Tușinean 🍇 https://code.sololearn.com/WgQvkfPNajv4/?ref=app
20th May 2020, 7:59 AM
piyush raj
piyush raj - avatar
+ 2
Hi You can use alert in function.If "If" loop executed then print alert("true") and for else print alert("false") .using alert you can check which condition is execcuted. function myFunction(a){ if(a % 2 == 0){ //some code alert("true"); } else{ alert("false"); }
20th May 2020, 9:33 AM
AjayGohil