Skip to content

cotrTernary

Creates a ternary operator

Aliases:

  • cotrTernary
  • ternary
  • conditionalOperator

cpp

condition ? trueValue : falseValue

csharp

condition ? trueValue : falseValue

dart

condition ? trueValue : falseValue

go

condition ? trueValue : falseValue

haskell

-- Haskell does not have a ternary operator.
-- Use a case expression or an if-then-else expression instead:
let result = case condition of
True -> trueValue
False -> falseValue

java

condition ? trueValue : falseValue

javascript

condition ? trueValue : falseValue

javascriptreact

condition ? trueValue : falseValue

kotlin

condition ? trueValue : falseValue

perl

condition ? trueValue : falseValue

php

condition ? trueValue : falseValue

powershell

Terminal window
condition ? trueValue : falseValue

python

trueValue if condition else falseValue

r

ifelse(condition, trueValue, falseValue)

ruby

condition ? true_value : false_value

rust

condition ? trueValue : falseValue

scala

// Scala does not have a ternary operator.
// Use an if-else expression instead:
val result = if (condition) trueValue else falseValue

shellscript

Terminal window
# Bash doesn't have a ternary operator, but you can use an if statement:
condition && trueValue || falseValue

swift

condition ? trueValue : falseValue

typescript

condition ? trueValue : falseValue

typescriptreact

condition ? trueValue : falseValue