null
null
means there is a value, and that value is nothing.
The value is intended to be nothing...meaning it was set to nothing with intention.
const userInfo = {
name: 'Nate',
email: 'nate@example.com',
secondaryEmail: null, // user has no secondary email
};
undefined
undefined
means the value isn't available (yet?).
It is an unambiguous indication that there may be something different there in the future.
const formInProgress = {
createdAt: new Date(),
data: new FormData(),
completedAt: undefined,
};
function submitForm() {
formInProgress.completedAt = new Date();
}
void
void
should be used exclusively to describe that a function's return value should be ignored.
console.log(`console.log returns nothing.`);
// (method) Console.log(...data: any[]): void
!
- Non-null assertion operator
The non-null assertion operator (!
) is used to cast away the possibility that a value might be null
or undefined
.
Overall recommended to NOT use this.
From the TypeScript Fundamentals, v3↗ course on FEM↗ taught by Mike North↗.