undefined

Last updated

What is undefined?

JavaScript has two primitive values used to signal absent or uninitialized value: null (absent) and undefined (uninitialized).

TypeScript has two corresponding types by the same names. How these types behave depends on whether you have the strictNullChecks option on.

With strictNullChecks off, values that might be null or undefined can still be accessed normally, and the values null and undefined can be assigned to a property of any type. This is similar to how languages without null checks (e.g. C#, Java) behave. The lack of checking for these values tends to be a major source of bugs; TypeScript always recommend people turn strictNullChecks on if it’s practical to do so in the codebase.

With strictNullChecks on, when a value is null or undefined, you will need to test for those values before using methods or properties on that value. Just like checking for undefined before using an optional property, we can use narrowing to check for values that might be null:

function doSomething(x: string | null) {
  if (x === null) {
    // do nothing
  } else {
    console.log('Hello, ' + x.toUpperCase());
  }
}

Free resources to learn undefined

More in stage 4: Type Compatibility

undefined: common questions

Can I learn undefined in Hindi?

This lesson is written in English. Once you sign in, open it in the interactive course and ask the AI tutor to explain undefined in Hindi or Hinglish. The tutor answers from this lesson’s own text, so it stays on the syllabus, and it runs on your own Google, OpenAI or Anthropic key.

Is this undefined tutorial for beginners?

It is lesson 22 of 93 in the free TypeScript roadmap, in stage 4, Type Compatibility. If you are new to TypeScript, start from stage 1 and come back here in order; if you already know the earlier stages, this lesson stands on its own.

Still confused? Ask me