Thread: Coding Thread
View Single Post
  #5  
Old 05-17-2016, 12:55 PM
Nazja Nazja is offline

Trade Baroness - Moderator
Nazja's Avatar
Join Date: Oct 2006
Location: All the lands of Wonder.
Posts: 40,959

Default

Quote:
Originally Posted by HlaaluStyle View Post
Double-post here, but I had a question.

I'm learning about comparisons and logical operators in JavaScript. They presented me with this:

<script>
function myFunction() {
var age, voteable;
age = document.getElementById("age").value;
voteable = (age < 18) ? "Too young":"Old enough";
document.getElementById("demo").innerHTML = voteable + " to vote.";
}
</script>

Looking at this and testing it, I'm not sure how the script knows to put "Too young" or "Old enough". Is it based on the location of the strings? The question mark? The colon?
If the part before ? is true (age < 18), then, the first option is chosen as the result of voteable; the person is too young. If it is false, then, the second option is chosen.

Basically:
Boolean ? true-option: false-option;
Reply With Quote