Null
- Pronunciation
- NUL
In short
Null is a special value that means "no value here", used when a variable deliberately refers to no object or data at all instead of holding something real.
What is null in programming?
Null is a marker that a variable intentionally holds no value. A missing middle name, a search that found no match or a database column left empty can all be represented with null. Languages spell it differently: null in Java, JavaScript, C# and Kotlin, None in Python, nil in Ruby, Go and Swift, and NULL or nullptr in C and C++.
Null exists because variables often refer to objects, and sometimes there is no object to refer to. The trouble starts when code assumes a value is present and tries to use it: calling a method on null raises errors like Java's NullPointerException or JavaScript's TypeError: Cannot read properties of null. Tony Hoare, who introduced null references in 1965, later called them his "billion-dollar mistake" because of how many bugs and crashes they have caused.
Modern languages reduce the risk by making absence explicit in the type system. In Kotlin, Swift, strict TypeScript and C# with nullable reference types, a type such as String? or string | null must be checked before use, and the compiler complains if you forget. Rust and Haskell have no null at all and use Option or Maybe types instead. Operators such as optional chaining (user?.address) and null coalescing (name ?? "Guest") keep these checks short.
Null is often confused with other "empty" values. An empty string, the number 0 and an empty list are real values, while null means there is no value at all. JavaScript adds another neighbor, undefined, which usually means a value was never assigned, whereas null is set on purpose. In SQL, NULL means unknown, so NULL = NULL is not true and you must test with IS NULL instead.
Key takeaways
- Null represents the deliberate absence of a value.
- Using null as if it were a real object causes some of the most common runtime errors.
- Kotlin, Swift, TypeScript and C# can track nullable types at compile time.
- Rust and Haskell avoid null entirely with
OptionandMaybetypes. - Null is different from empty values such as
"",0or an empty list.
Example
interface User {
name: string;
nickname: string | null; // may be missing on purpose
}
function displayName(user: User): string {
// user.nickname.toUpperCase() would not compile: it might be null
return user.nickname ?? user.name;
}
displayName({ name: "Ada", nickname: null }); // "Ada"
displayName({ name: "Grace", nickname: "Amazing" }); // "Amazing"Readers ask
What is the difference between null and undefined in JavaScript?
undefined usually means a variable or property was never given a value, while null is assigned deliberately to mean "no value". null == undefined is true, but null === undefined is false because they are different types.
What is a null pointer exception?
It's the error raised when code tries to use a null reference as if it pointed to a real object, for example by calling a method on it. Java calls it NullPointerException, and other languages have similar errors under different names.
Why is null called the billion-dollar mistake?
Computer scientist Tony Hoare, who added null references to the ALGOL W language in 1965, used that phrase in 2009 to describe the huge cost of the crashes and bugs null has caused. Many newer languages therefore require missing values to be handled explicitly.
See also
- Data TypeProgramming Fundamentals, p. 14A data type is a classification that tells a program what kind of value a piece of data holds, such as a number or text, and which operations work on it.
- VariableProgramming Fundamentals, p. 56A variable is a named storage location in a program that holds a value, such as a number or a piece of text, which the code can read and change as it runs.
- PointerProgramming Fundamentals, p. 44A pointer is a variable that stores the memory address of another value instead of the value itself, letting code read or change that data indirectly.
- ExceptionProgramming Fundamentals, p. 19An exception is an event that interrupts a program's normal flow when an error occurs, which code can catch and handle instead of letting the program crash.
- KotlinProgramming Languages, p. 18Kotlin is a concise, statically typed language that runs on the JVM, works seamlessly with Java, and is widely used for Android and server-side development.
- TypeScriptWeb Development, p. 50TypeScript is a programming language built on JavaScript that adds static types, catching many bugs before the code runs, and compiles to plain JavaScript.
Spotted a mistake or something missing on this page?Suggest an edit