Get a Quote Right Now

Edit Template

Arrays

TypeScript is to be a static typechecker for JavaScript programs – in other words, a tool that runs before your code runs (static) and ensures that the types of the program are correct (typechecked). Arrays: Arrays in TypeScript are similar to arrays in JavaScript, but with the added benefit of type annotations and type inference. To specify the type of an array like [1, 2, 3], you can use the syntax number[]; Array Declaration: let numbers: number[] = [1, 2, 3, 4, 5];let strings: string[] = [“apple”, “banana”, “cherry”]; Type Inference: TypeScript can often infer the type of the array based on the values you initialize it with, so you don’t always need to explicitly specify the type.let inferredNumbers = [1, 2, 3, 4, 5]; // TypeScript infers type as number[] Array Iteration: You can iterate through arrays using loops or methods like forEach, map, filter, etc.let numbers: number[] = [1, 2, 3];numbers.forEach(num => console.log(num));let doubledNumbers = numbers.map(num => num * 2); Array Type Union: You can create arrays that contain values of multiple types using type unions.let mixed: (number | string)[] = [1, “two”, 3, “four”]; Array Generics:TypeScript also supports array generics to define arrays with a specific type parameter.let arrayOfStrings: Array = [“hello”, “world”];

Type Coercion

Type Coercion:- Type coercion, also known as type conversion, refers to the process of automatic or implicit conversion of values from one data type to another. This is often necessary when performing operations involving values of different types. Type coercion can be either explicit or implicit. For example, in JavaScript: var x = 5; var y = “10”; var result = x + y; // result will be “510” due to string concatenation In this example, the number 5 is implicitly coerced into a string, and then the string concatenation operation is performed.

Closures

Closures:- Since a nested function is a closure, this means that a nested function can “inherit” the arguments and variables of its containing function. In other words, the inner function contains the scope of the outer function. The closures can contain multiple scopes; they recursively contain the scope of the functions containing it. This is called scope chaining. (The reason it is called “chaining” is explained later+). Closures provide encapsulation for the variables of the inner functions. Consider the following example: In this example, C accesses B‘s y and A‘s x. This can be done because: The reverse, however, is not true. A cannot access C, because A cannot access any argument or variable of B, which C is a variable of. Thus, C remains private to only B

Recursion

Recursion:A function that calls itself is called a recursive function. In some ways, recursion is analogous to a loop. Both execute the same code multiple times, and both require a condition (to avoid an infinite loop, or rather, infinite recursion in this case). For example, consider the following loop: This is while loop syntax to print //0123456789 let x = 0;// “x < 10” is the loop conditionwhile (x < 10) {// do stuffx++;} This is recursion way to print //0123456789function loop(x) {// “x >= 10” is the exit condition (equivalent to “!(x < 10)”)if (x >= 10) {return;}// do stuffconsole.log(x);loop(x + 1); // the recursive call}loop(0);

Exception Handling

You can throw exceptions using the throw statement and handle them using the try…catch statements. throw statement:- throw “Error2”; // String typethrow 42; // Number typethrow true; // Boolean typethrow {toString() {return “I’m an object!”;},}; try…catch statement:-