Abstract
- Handles situations where we try to access non-existent resources gracefully
- For example, it prevents issues like Segmentation Fault by shifting error detection from runtime to compile-time
Optional Datatype
- A special datatype designed to achieve null safety
- The
Optionaldatatype can either contain a value or be empty - When a programmer writing his/her codes, the Compiler will only compile if he/she handles the
Optionalexplicitly - This feature is supported only in languages that provide both Generics and Compilation capabilities
Example
Safe Navigation
- Also known as Optional Chaining
- Provides a safe way to interact with potentially null or undefined values at runtime. When a
nullorundefinedis encountered, the program short-circuits and safely returnsundefinedinstead of throwing an error.
Example
In Node.js, you can place a
?(optional chaining operator) after a variable or property before accessing its value. If the variable or property isnullorundefined, it will returnundefinedinstead of causing the program to crash.
Tip
When writing code, programmers can use Code Quality Assurance Tools such as Linters to identify code patterns that might lead to unexpected
undefinedvalues and ensure better code quality.
