Skip to content

Shorthand Notations in TypeScript and JavaScript

Shorthand notations provide a way to write more concise and readable code in TypeScript and JavaScript. Here are some of the most commonly used shorthand notations, along with examples and use cases.

ShorthandDescriptionExampleUse case
+Converts a string to a numberconst idNumber = +id;When converting route parameters from strings to numbers
?.Allows safe access to nested properties and methodsconst name = user?.address?.city;When accessing properties or methods that may be null or undefined
??Provides a default value for null or undefined variablesconst name = username ?? 'Anonymous';When specifying a default value for optional parameters
...Expands an array or object into individual elementsconst combinedArray = [...array1, ...array2];When combining arrays or objects, or when passing multiple arguments to a function
Destructuring assignmentExtracts values from complex objects or arraysconst { name, age, address: { city } } = user;When extracting values from complex objects or arrays