Skip to content

Do expressions

Baseline logo Baseline
Chrome logo Edge logo
Firefox logo Zen logo
Safari logo
Node logo
Bun logo
Deno logo
Cloudflare Workers logo

Assign values based on a condition with better readability than ternaries.

Type TC39 CoreJS Babel Civet JS+
Syntax Stage 0 View View View via Babel
JS+ TypeScript
const x = 100;
const y = 20;
const a = do {
if (x > 10) {
if (y > 20) {
'big x, big y';
} else {
'big x, small y';
}
} else {
if (y > 10) {
'small x, big y';
} else {
'small x, small y';
}
}
};
const x = 100;
const y = 20;
const a = x > 10
? y > 20
? 'big x, big y'
: 'big x, small y'
: y > 10
? 'small x, big y'
: 'small x, small y';