简体   繁体   中英

True and False return of functions in Javascript

I would like to display the message “Yes” or “No” for getting a function return of true or false respectively (not directly changing the word true and false) Display but not using alert.

How can I do that? Thanks.

function myFunction {
  if (Math.floor(Math.random()*2) == 0) return false
  else return true;
}
if (myFunction()) { console.log("Yes"); }
else { console.log("No"); }

I see you are new, so welcome :v --

Way 1: document.write(myFunction()? 'yes' : 'no');

Way2:

<p id="demo" ></p>
<button onclick="myFunction()"> yes or no </button>

<script>
function myFunction() {
  document.getElementById("demo").innerHTML = myFunction()? 'yes' : 'no';
}
</script>

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