Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Conditional (computer programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== JavaScript === [[JavaScript]] uses if-else statements similar to those in [[C syntax|C]] languages. A Boolean value is accepted within parentheses between the reserved ''if'' keyword and a left curly bracket. <syntaxhighlight lang="javascript"> if (Math.random() < 0.5) { console.log("You got Heads!"); } else { console.log("You got Tails!"); } </syntaxhighlight> The above example takes the conditional of <code>Math.random() < 0.5</code> which outputs <code>true</code> if a random float value between 0 and 1 is greater than 0.5. The statement uses it to randomly choose between outputting <code>You got Heads!</code> or <code>You got Tails!</code> to the console. Else and else-if statements can also be chained after the curly bracket of the statement preceding it as many times as necessary, as shown below: <syntaxhighlight lang="javascript"> var x = Math.random(); if (x < 1/3) { console.log("One person won!"); } else if (x < 2/3) { console.log("Two people won!"); } else { console.log("It's a three-way tie!"); } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)