How to add if and else condition with function in JavaScript | SoloLearn: Learn to code for FREE!

+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

5/20/2020 6:17:10 AM

piyush raj

3 Answers

New Answer

+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 }

+2

got it thank you @💜 Alex Tușinean 🍇 https://code.sololearn.com/WgQvkfPNajv4/?ref=app

+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"); }