Nullable

2 snippets across 2 stacks — Python, TypeScript

PYPython

None (Null Value)

PY · Variables & Types
syntax
variable = None
example
result = None
if result is None:
    print("No result yet")

# Common pattern: optional return
def find_user(user_id: int) -> str | None:
    return None
output
No result yet

Note Always compare to None with 'is' or 'is not', never == or !=. None is a singleton object.

TSTypeScript

null & undefined

TS · Basic Types
syntax
let varName: null = null;
let varName: undefined = undefined;
let varName: string | null = null;
example
let resetValue: null = null;
let notAssigned: undefined = undefined;

// Practical usage — nullable types
let selectedUserId: string | null = null;
selectedUserId = "usr_482";
output
// selectedUserId can hold either a string or null

Note With strictNullChecks enabled (recommended), null and undefined are NOT assignable to other types unless you explicitly include them in a union. This catches a huge class of runtime bugs.