Table of Contents
null vs undefined
Link to heading
In TypeScript, undefined means a variable has been declared but has not yet been assigned a value, while null represents an intentional, explicit absence of any object value.
When used in optional parameters and properties, TypeScript inherently favors undefined for optional features. When you mark an object property or a function argument as optional using the ? modifier, TypeScript implicitly appends | undefined to its underlying type. It does not append null.
If a function parameter has a default fallback value, passing undefined forces the parameter to use that default value. Passing null overrides the default and preserves the null state.
Example:
const greet = (name = "Guest") => console.log(`Hello, ${name}`);
greet(undefined); // Logs: "Hello, Guest"
greet(null); // Logs: "Hello, null"
javascript array empty slots vs undefined
Link to heading
Empty slots will be created when initiated an array. Either use new Array(number) or let arr = [1, 2, 3]; arr[50] = 100;, the 2 examples will generate empty slots.
Emtpy slots, unlike undefined, will be skipped by some generic functions, like map and reduce. So if you want to make it iterate every elements, then you have to explicitly at least declare undefined.
tsconfig.json
Link to heading
tsconfig.json is the configuration file for typescript compiler (tsc). In this file, it usually tells rules for compiler to enforce: target is the standard for javascript; what syntax to import and export, etc.
template literals ` Link to heading
Template literals use backticks (`) instead of normal quotes. You can embed variables or expressions directly into the string using the ${expression} syntax.
rest parameters Link to heading
Rest parameters are much alike python’s *args: not regulating how many variables to pass into a function. In javascript, it’s ...restParameters, and typescript will make it type safe, like ...restParameters: string[].