Site menu

CodeCook.io

  • Languages
    • C
    • C++
    • CSS
    • Git
    • HTML
    • Java
    • JavaScript
    • jQuery
    • PHP
    • Python
    • Ruby
    • Scala
  • control
  • if
  • ternary

JavaScript - Inline conditional

Execute one or other code block depending on condition in a single line. This ternary conditional code is shorter than explicit if statements.

0
673
  • Share on Facebook
  • Share on Google+
  • Share on Twitter

Using native ternary operator

Either the true or false code is evaluated based on the condition.

{{condition}} ? {{true_code}} : {{false_code}}
javascript
  • ternary conditional

Notes

  • true and false branch code may be expression or statement.
  • branch code may only be single line.

last update: Feb. 8, 2015

Advertisement

This project is hosted on DigitalOcean, try it and support us

Related concept

  • Break out of loop
  • If, else if, else

Other language

  • C++
  • Scala
  • Python
  • C
  • PHP

Random task

  • Remove duplicates from list
  • Clearfix
  • Check if specific commit is on remote
  • Stash apply by label
  • List files with merge conflict
  • Blog
  • About
  • License