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.

Frequently asked questions

How does Python handle nullable?
This task is covered in 2 stacks on this page: Python, TypeScript. The "None (Null Value)" snippet in Python uses `variable = None`.
Which code does the Python example use?
The "None (Null Value)" snippet uses `variable = None`, from the Variables & Types section of the Python cheat sheet.
Which stacks cover "nullable" on this page?
Python, TypeScript. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "None (Null Value)": Always compare to None with 'is' or 'is not', never == or !=. None is a singleton object.