55

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.

neomib
  • 3,503
  • 4
  • 17
  • 27
  • 3
    The differences are the same as in JavaScript: https://stackoverflow.com/q/5076944/6680611 – cartant Jun 14 '17 at 05:47
  • 6
    @cartant I don't think that it is right to see that in Typescript it is the same as in javascript. As a fact the Typescript Styleguide ( https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines#null-and-undefined ) says to only use undefined. The reason is that in typescript a question mark is optional. The optional is with undefined and not null. Meaning that there is a big difference between the two. Like that Typescript is a superset of javascript so everything in javascript is legitimate. But ideally, you shouldn't use null in typescript. – Elisha Sterngold Mar 14 '19 at 14:11
  • @ElishaSterngold Read the first section of the style guide, it's only for contributers to TypeScript itself. There's nothing wrong with using `null` in TypeScript for your own projects. Also, any stylistic reason to choose one over the other would be equally valid in JS anyway. – John Montgomery May 05 '19 at 06:01
  • @JohnMontgomery That the styleguide decided something is with a reason. I already explained the reason in my comment. – Elisha Sterngold May 06 '19 at 07:03
  • @ElishaSterngold Yes, and that reason is "We have chosen many of them for team consistency." They say *twice* that it's not a guide for the TypeScript community. – John Montgomery May 06 '19 at 07:38
  • @JohnMontgomery I disagree – Elisha Sterngold May 06 '19 at 16:33
  • @ElishaSterngold You disagree with what the guide says? – John Montgomery May 06 '19 at 19:28
  • 1
    As much as typescript is a superset of javascript, I strongly disagree to treat them as equal. And it's not just for stylistic reasons. There is an ideological difference. In javascript's dynamic nature, we want to differentiate non-existent keys and missing values. With typing and interfacing used properly in typescript, we should know if certain keys exists (whether there is a value or not) with the type-checker. This makes having 2 different "empty" values obsolete and eliminating one of them makes things much cleaner. And we should keep undefined because of all other reasons mentioned. – lhhong Sep 23 '20 at 06:11

2 Answers2

32

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.

moffeltje
  • 4,521
  • 4
  • 33
  • 57
Spitzbueb
  • 5,233
  • 1
  • 20
  • 38
  • I can imagine it's because of either the checking (e.g. in if statements) or just to be consistent. – Spitzbueb Aug 28 '17 at 15:14
  • @AlexanderAbakumov [Here's](https://basarat.gitbooks.io/typescript/docs/tips/null.html) an explanation. – pushkin Feb 20 '18 at 18:12
  • 30
    Note that that style guide is specifically for people contributing to TypeScript so that it's consistent, and explicitly *not for TypeScript programmers in general.* Do whatever you want for your own project, or whatever your team agrees on. – John Montgomery Jun 30 '18 at 00:56
  • 3
    @Pipo Maybe [this](https://github.com/basarat/typescript-book/blob/master/docs/javascript/null-undefined.md) is what I was linking to. Could be different though – pushkin Jan 27 '19 at 16:34
12

The value 'undefined' denotes that a variable has been declared, but hasn't been assigned any value. So, the value of the variable is 'undefined'.

On the other hand, 'null' refers to a non-existent object, which basically means 'empty' or 'nothing'.

You can manually assign the value 'undefined' to a variable, but that isn't recommended. So, 'null' is assigned to a variable to specify that the variable doesn't contain any value or is empty. But 'undefined' is used to check whether the variable has been assigned any value after declaration.