Search code examples
javascripttypescriptnullundefined

Typescript - What is the difference between null and undefined?


I want to know what is the difference between null and undefined in typescript. I know in javascript it is possible to use both of them in order to check a variable has no value. But in typescript I want to know the difference exactly and when it is better to use each one of them. Thanks.


Solution

  • This post explains the differences very well. They are the same in TypeScript as in JavaScript.

    As for what you should use: You may define that on your own. You may use either, just be aware of the differences and it might make sense to be consistent.

    The TypeScript coding style guide for the TypeScript source code (not an official "how to use TypeScript" guide) states that you should always use undefined and not null: Typescript Project Styleguide.