Javascript - Undefined

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)





Discover More
Javascript Function Method
Javascript - Functions

Functions functionalities in Javascript. A function in Javascript is a variable that’s given a reference to the function being declared. The function itself is a value (as an integer or an array) that...
Javascript - Null

null is a javascript data type that is unfortunately reported as an object by the typeof operator. nullno object valueundefinedno value typeof null returns object, and not null, despite Null being a...
Javascript - Primitive Data Type

Javascript has 6 primitive data type. string number boolean null undefined symbol Except for null and undefined, all primitive values have object equivalents that wrap around the primitive...
Chrome Dev Tool Source Debugger Scope
Javascript - This

this is a variable that has a reference value to the scope (namespace) this printThisobjthisobj Generally, this is the local scope but it may be changed. You can set this : in the call of a function...
Javascript - Type

Type in Javascript Javascript is not static typed but it exist Javascript wrapper language that implements it. See Only values have types in JavaScript; variables are just simple containers for those...
Javascript Object - JSON.Stringify

JSON.stringify transform an object as a string with a JSON structure A JSON string from a javascript object How to transform a set to an array By returning undefined from the replacer, you...



Share this page:
Follow us:
Task Runner