Skip to content

Latest commit

 

History

History
83 lines (75 loc) · 2.95 KB

optional-input.md

File metadata and controls

83 lines (75 loc) · 2.95 KB

Optional Input

An optional input or optional parameter is a function parameter that has a default value.

If the caller doesn't pass in a value for the parameter, the function uses the default value.

For example:

// `suffix` is an optional input
export function addSuffix(path: string, suffix: string = ".json") {
    return path + suffix;
}

// the caller can pass in a value if they want
// path1 == "./settings.yaml"
const path1 = addPath("./settings", ".yaml");

// if the caller does not pass in a value,
// the default value is used
// path2 == "./settings.json"
const path2 = addPath("./settings");

Typescript supports optional inputs. They were added to Javascript as part of ES2015. Some programming languages (noteably Golang) do not support optional inputs.