Backbencher.dev

Confirm Presense of Value Using TypeScript !

Last updated on 2 Jan, 2023

Consider following line of code:

const input1 = document.getElementById("num1");

There is a chance that the id num1 does not exist. Due to that reason, TypeScript will throw an error. Now if we are sure that the element with id num1 will be present, we can tell that to TypeScript using exclamation mark !.

const input1 = document.getElementById("num1")!;

The ending ! tells that always there will be a value for document.getElementById("num1").

--- ○ ---
Joby Joseph
Web Architect