Skip to content

Use-Ternary-Operator

Documentation Index

Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt

Use this file to discover all available pages before exploring further.

Replace if statement with ternary operator

if (cond) {
var x = 1
} else {
var x = 2
}
var x = cond ? 1 : 2;

The ternary operator is a simpler and more elegant way to write conditional assignments to a variable than the long form.