Table of Contents

About

When the value of variable or property is not defined, it gets the type undefined.

It's handy to see if a variable is passed as an argument of a function.

Are undefined:

  • a not declared variable
  • a declared variable without value
  • functions that return no values
  • the void operator.

How to test if a variable is undefined

With the strict equality operator

let sec;
if (sec === undefined) console.log("The variable sec is not defined");

With the loose equality operator

let sec;
// == and not ===
if (sec == null) console.log("The variable sec is not defined");

With the typeof output

if (typeof sec === 'undefined') console.log("The variable sec is not defined");

Typescript: Question Mark

The question mark means optional and permits declaring that a variable may be undefined.

For instance, if location may be undefined, you can write:

country = location?.getCountry()

1)