Defining and using functions
When Node.js processes a JavaScript file, it executes the statements in the order in which they have been defined. In common with most languages, JavaScript allows statements to be grouped into a function, which won’t be executed until a statement that invokes the function is executed, as shown in Listing 3.34.
Listing 3.34. Defining a function in the index.ts File in the primer Folderfunction writeValue(val: string | null) {
console.log(`Value: ${val ?? "Fallback value"}`)
}
writeValue("London");
writeValue(null);
Functions are defined with the function
keyword and are given a name. If a function defines parameters, then TypeScript requires type annotations, which are used to enforce consistency in the use of the function. The function in Listing 3.34 is named writeValue
, and it defines a parameter that will accept string
or null
values. The statement inside of the function isn’t executed until a statement that invokes...