Ternary & Special Operators in JavaScript

From the JavaScript cheat sheet · Operators · verified Jul 2026

Ternary & Special Operators

Conditional and other special operators

javascript
// Ternary operator
const result = condition ? 'yes' : 'no';

// Optional chaining
const value = obj?.property?.nested;
const result = func?.();

// Nullish coalescing
const value = input ?? 'default';

// Spread operator
const arr = [...array1, ...array2];
const obj = {...obj1, ...obj2};

// Comma operator
let x = (5, 10); // x = 10

// void operator
void functionCall(); // returns undefined
💡 Ternary operator is great for simple conditions, avoid nesting
⚡ Optional chaining prevents "Cannot read property of undefined" errors
📌 Spread creates shallow copies, not deep copies
🔥 Comma operator evaluates all expressions, returns the last one

Continue with JavaScript

Save the full cheat sheet or work through every related task.

More JavaScript tasks

Back to the full JavaScript cheat sheet