PROGRAMMING-CONCEPTS

Null, Undefined, and None: Definition, Purpose, and Examples

Null, undefined, and none represent the idea of “no value,” but each language expresses that idea differently. You will see them constantly when handling missing data, optional values, failed lookups, and functions that don’t return anything meaningful. Understanding how these values behave helps you avoid confusing bugs and write safer programs.


What These Values Represent

Every programming language needs a way to express the absence of a value. Instead of inventing a separate term in each language, most rely on a small set of built-in values:

  • None in Python
  • null and undefined in JavaScript / TypeScript
  • nil in Swift
  • NULL in SQL

All of them communicate one idea: “There is nothing here.”

Even though the concept is shared, the behavior is language-specific — especially between null and undefined in JavaScript.


Python: None

Python uses a single keyword: None.

It represents the absence of a value, a missing result, or an intentional “empty” placeholder.

result = None

This often appears when a function doesn’t have anything useful to return:

def log_message(msg):
    print(msg)
    # No return here → Python returns None automatically

Functions that return None often exist for their side effects (printing, writing to a file, updating a database), not for producing new data.

A common case: checking if a lookup succeeded.

user = database.get("alice")  # could be a dictionary or ORM
if user is None:
    print("User not found")

Python’s is None check is intentional — object identity, not equality, avoids false positives.


JavaScript: null vs undefined

JavaScript has two ways to represent “nothing,” and they behave differently:

undefined

  • Something exists… but has no assigned value yet
  • Default result of a function with no return
  • Default value of missing object properties
let user;
console.log(user);   // undefined

Objects return undefined when accessing missing fields:

const car = { brand: "Toyota" };
car.model;   // undefined

A function without a return statement also evaluates to undefined:

function sum(a, b) {
  const total = a + b;
}
sum(1, 2);   // undefined

null

  • A deliberate assignment meaning “no value”
  • Developers use it when they want to say something is empty on purpose
let selected = null;   // explicit “nothing selected”

This distinction matters because many bugs come from mixing the two.

JavaScript programmers often use optional chaining to guard against null and undefined:

const username = user?.profile?.name;

This prevents errors when a nested property is missing.


TypeScript: null, undefined, and strict checking

TypeScript inherits JavaScript’s two null values but adds type safety.

Example:

let message: string | null = null;

You must explicitly allow null or undefined if your code might produce them:

let count: number;   // implicitly undefined

With strictNullChecks enabled, TypeScript helps prevent runtime crashes by forcing you to handle null and undefined before using a value.

function getLength(str: string | undefined) {
  if (str === undefined) return 0;
  return str.length;
}

TypeScript turns null-safety into a compile-time skill instead of a debugging chore.


Swift: nil and Optionals

Swift uses nil to represent missing values, but only inside an Optional type.

var name: String? = nil     // Optional String that currently has no value

Before using an optional value, Swift requires you to unwrap it:

if let actualName = name {
    print(actualName)
} else {
    print("No name provided")
}

This design prevents many common null-pointer bugs because the compiler forces you to acknowledge missing values.

Swift’s optionals are one of the safest implementations of “null” across modern languages.


SQL: NULL

In SQL, NULL is a placeholder for missing data, not zero or an empty string. Many newcomers are surprised by how NULL behaves in comparisons:

SELECT * FROM users WHERE last_login = NULL;   -- Always false!

The correct check uses IS NULL:

SELECT * FROM users WHERE last_login IS NULL;

Any calculation involving NULL returns NULL:

SELECT 5 + NULL;   -- NULL

This ruleset reflects database logic: unknown values poison expressions unless explicitly handled.


How These Values Affect Logic and Conditionals

Missing values often influence program flow.

Python

if value is None:
    print("No data provided")

JavaScript

Using == collapses null and undefined into the same check:

value == null   // true for both null and undefined

But strict equality distinguishes them:

value === null       // only null
value === undefined  // only undefined

Developers choose based on whether the distinction matters.

Swift

The language forces safe access:

if let data = response {
    // safe to use data
}

This prevents accidental crashes.


Real-World Example: API Responses

APIs frequently return null-like values when data is incomplete.

JavaScript (React)

if (!user?.email) {
  return <p>No email on file.</p>;
}

Optional chaining shields the UI from undefined values during slow network responses.

Python (FastAPI / Django)

email = user.get("email")
if email is None:
    return {"error": "Email missing"}

APIs rely on explicit None checks to validate fields.


Real-World Example: Form Input

JavaScript

function parseAge(input) {
  if (input === "") return null;   // user left it blank on purpose
  return Number(input) || undefined;  // convert or mark as invalid
}

Null communicates “nothing,” undefined communicates “invalid or unassigned.”

Swift

let age = Int(textField.text ?? "")   // returns nil if conversion fails

Swift’s Optional type encodes failure cleanly.


Why These Values Cause Bugs

Some common pitfalls:

  • Treating undefined and null as interchangeable when they’re not
  • Forgetting that Python’s None is falsy but not the same as 0 or ""
  • Using == in JavaScript and unintentionally matching both null and undefined
  • Comparing SQL NULL with = instead of IS NULL
  • Forgetting to unwrap Swift optionals

Most beginner bugs involving missing data come from misunderstanding how each language represents absence.


Best Practices

  • Use None, null, or nil intentionally. Avoid relying on defaults.
  • In JavaScript/TypeScript, prefer strict equality (===).
  • Use optional chaining to prevent crashes in nested lookups.
  • In Swift, embrace optionals rather than force-unwrapping.
  • In SQL, always use IS NULL, not =.
  • When designing APIs, document which fields may return null.

Clear handling of missing values is a hallmark of reliable code.


Summary

Null, undefined, None, and nil are different representations of a single idea: absence. Each programming language handles that absence with its own rules, safety checks, and behaviors. Whether you are validating input, reading API responses, querying databases, or structuring components in React, handling missing values correctly prevents subtle logic errors and makes your programs more predictable.

Learn to Code for Free
Start learning now
button icon
To advance beyond this tutorial and learn to code by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

Reach your coding goals faster