In operator

8 snippets across 4 stacks — JavaScript, Python, Regular Expressions, TypeScript

Also written as := operator · ?? operator · ?. operator · or operator · ? : operator · ??= operator

JSJavaScript

Nullish Coalescing (??)

JS · Data Types
syntax
value ?? fallback
example
const inputCount = 0;
console.log(inputCount || 10);  // 10 (wrong!)
console.log(inputCount ?? 10);  // 0  (correct)

const label = null;
console.log(label ?? "Untitled");
output
10
0
"Untitled"

Note Only triggers on null/undefined, unlike || which triggers on all falsy values (0, "", false, NaN). Use ?? when 0 or empty string are valid values.

Optional Chaining (?.)

JS · Data Types
syntax
obj?.prop
obj?.[expr]
obj?.method()
example
const user = { profile: { avatar: "pic.png" } };
console.log(user?.profile?.avatar);  // "pic.png"
console.log(user?.settings?.theme);  // undefined
console.log(user?.getName?.());      // undefined (no error)

Note Short-circuits to undefined when a link in the chain is null/undefined. Combine with ?? for defaults: user?.name ?? "Guest"

Nullish Assignment (??=)

JS · Data Types
syntax
variable ??= value;
example
let config = { timeout: null, retries: 3 };
config.timeout ??= 5000;
config.retries ??= 10;
console.log(config);
output
{ timeout: 5000, retries: 3 }

Note Only assigns when the left side is null or undefined. Also available: ||= (assigns on falsy) and &&= (assigns on truthy).

Checking Property Existence

JS · Objects
syntax
Object.hasOwn(obj, prop)
"prop" in obj
example
const user = { name: "Lina", age: 28 };

console.log(Object.hasOwn(user, "name")); // true
console.log(Object.hasOwn(user, "email")); // false
console.log("toString" in user);           // true (inherited)
console.log(Object.hasOwn(user, "toString")); // false

Note Object.hasOwn() (ES2022) replaces obj.hasOwnProperty(). It works even if the object was created with Object.create(null).

Ternary Operator

JS · Control Flow
syntax
condition ? valueIfTrue : valueIfFalse
example
const age = 20;
const category = age >= 18 ? "adult" : "minor";
console.log(category); // "adult"

// Nested (use sparingly)
const tier = age >= 65 ? "senior" : age >= 18 ? "adult" : "minor";

Note Great for simple inline conditions. Avoid deeply nested ternaries -- they quickly become unreadable. Use if/else for complex logic.

PYPython

Walrus Operator (:=)

PY · Control Flow
syntax
if (name := expression):
example
data = [5, 12, 3, 18, 7, 22]
high = [x for x in data if (doubled := x * 2) > 20]
print(high)

import re
text = "Order #12345 confirmed"
if (match := re.search(r"#(\d+)", text)):
    print(f"Order ID: {match.group(1)}")
output
[12, 18, 22]
Order ID: 12345

Note The walrus operator (Python 3.8+) assigns and returns a value in one step. Most useful in while-loops, if-statements, and comprehensions to avoid repeated computation.

RXRegular Expressions

Alternation |

RX · Groups & Alternation
syntax
pattern1|pattern2
example
JS:  'My cat and your dog'.match(/cat|dog/g)
Py:  re.findall(r'\b(?:jpg|png|gif)\b', 'files: logo.png and bg.jpg')
output
JS: ["cat", "dog"]
Py: ["png", "jpg"]

Note The | operator has very low precedence -- /ab|cd/ matches 'ab' or 'cd', NOT 'a(b|c)d'. Always wrap alternations in a group when they are part of a larger pattern to avoid surprises.

TSTypeScript

in Operator Guard

TS · Type Guards & Narrowing
syntax
if ("property" in value) { ... }
example
interface EmailContact {
  email: string;
  name: string;
}

interface PhoneContact {
  phone: string;
  name: string;
}

function sendMessage(contact: EmailContact | PhoneContact) {
  if ("email" in contact) {
    console.log(`Emailing ${contact.email}`);
  } else {
    console.log(`Calling ${contact.phone}`);
  }
}
output
// 'in' narrows based on the presence of a property

Note The 'in' operator checks if a property name exists on an object at runtime. TypeScript uses this to narrow union types. The property name must be a string literal for narrowing to work. Works well when union members have distinct unique properties.