TypeScript Flashcards Preview

Computer Programming > TypeScript > Flashcards

Flashcards in TypeScript Deck (53)
Loading flashcards...

ECMA Script 2015 introduced which two new keywords for declaring variables, and constants ?

let , const


What is the main difference between declaring a variable using keyword "var" or using keywords "let" or "const" ?

The main difference is in the scoping rules applied to the identifiers declared with them. Variables using "var" are globally available within the function they were declared in, while variables declared with "let" and "const" are local to the block they were declared in.


How can one identify a "block" ?

Usually a block is defined by a set of curly braces.


What is another difference between variables declared with "var" and variables declared with "let" and "const" ?

Variables declared with "var" are hoisted to the top of the function they were declared in, meaning, the run time behavior will be as if it was declared on the top even if it was declared on the bottom of a function. Variables declared with "let" and "const" are only available where they were declared.


What is the main difference between declaring a variable using keyword "var" or using keywords "let" or "const" ?

When using "var" , it is possible to declare the same variable name twice in the same function. This is usually not good practice, and with "let" and "const" you can only declare one name per block, avoiding this problem all-together.


What are the different "types" used in TypeScript ?

Boolean, Number, String, Array, Tuple, Enum, Any, Void, Null, Undefined, and Never.


What is a boolean's value ?

True or false .

let isDone: boolean = false;


What is a number's value ?

Any integer. It is not treated like a string !

let decimal: number = 6;
let hex: number = 0xf00d;
let binary: number = 0b1010;
let octal: number = 0o744;


What is a string's value ?

Anything between " " .

let color: string = "blue";
color = 'red';


What is an array's value ?

A collection of more then 1 of a certain type .

let list: number[ ] = [1, 2, 3];

--this is a more generic way--
let list: Array = [1, 2, 3];


What is a Tuple's value ?

Tuple types allow you to express an array where the type of a fixed number of elements is known, but need not be the same. For example, you may want to represent a value as a pair of a string and a number:

// Declare a tuple type
let x: [string, number];
// Initialize it
x = ["hello", 10]; // OK
// Initialize it incorrectly
x = [10, "hello"]; // Error


What is a Enum's value ?

An enum is a way of giving more friendly names to sets of numeric values.

enum Color {Red, Green, Blue};
let c: Color = Color.Green;


Enums begin numbering their members starting at what ?

Enums start numbering at 0, but you can change that by manually setting the value of one of its members, or all of its members.

enum Color {Red = 1, Green = 2, Blue = 4};
let c: Color = Color.Green;


What is a handy feature of Enums ?

You can also go from a numeric value to the name of that value in the enum. For example, if we had the value 2 but weren’t sure what that mapped to in the Color enum we could look up the corresponding name:

enum Color {Red = 1, Green, Blue};
let colorName: string = Color[2];



What is Any's value ?

It's any value ! Allowing you to gradually opt-in and opt-out of type-checking during compilation.


What is Void's value ?

the opposite of any: the absence of having any type at all. You may commonly see this as the return type of functions that do not return a value:

function warnUser(): void {
alert("This is my warning message");


What is Null and Undefined ?

By default null and undefined are subtypes of all other types. That means you can assign null and undefined to something like number.


What is Never's value ?

The never type represents the type of values that never occur. For instance, never is the return type for a function expression or an arrow function expression that always throws an exception or one that never returns.


How do you assign a type to a variable ?

: type // after the variable


What happens if you don't assign a type ?

TypeScript will infer one based on how you used the variable.


How do you assign a type to the return value of a function ?

: type // after the ) of the function parameters.


Find the problem :

function sumMatrix(matrix: number[ ][ ]) {
var sum = 0;
for (var i = 0; i < matrix.length; i++) {
var currentRow = matrix[i];
for (var i = 0; i < currentRow.length; i++) {
sum += currentRow[i];

return sum;

The inner for-loop will accidentally overwrite the variable i because i refers to the same function-scoped variable.


What is the output of the following ? Why ?

for (var i = 0; i < 10; i++) {
setTimeout(function() { console.log(i); }, 100 * i);

The output will be 10, 10, 10 etc. The reason : setTimeout will run a function after some number of milliseconds(though waiting for anything else to stop running), but only after the for loop has stopped executing; By the time the for loop has stopped executing, the value of i is 10. So each time the given function gets called, it will print out 10!


How do you avoid a 10, 10, 10 with setTimeout ?

Immediately Invoked Function Expression:

for (var i = 0; i < 10; i++) {
// capture the current state of 'i'
// by invoking a function with its current value
(function(i) {
setTimeout(function() { console.log(i); }, 100 * i);


How are "let" statements written ?

let hello = "Hello!";


What is lexical-scoping or better know as block-scoping ?

Unlike variables declared with var whose scopes leak out to their containing function, block-scoped variables are not visible outside of their nearest containing block or for-loop.


What is the "temporal dead zone" ?

This is just a sophisticated way of saying you can’t access the variable before the let statement, and luckily TypeScript will let you know that.


What is "shadowing" ?

The act of introducing a new name in a more nested scope.


What is the output of the following? Why ?

for (let i = 0; i < 10 ; i++) {
setTimeout(function() { console.log(i); }, 100 * i);

0, 1, 2, 3.. etc The reason : "let" declarations have drastically different behavior when declared as part of a loop. Rather than just introducing a new environment to the loop itself, these declarations sort of create a new scope per iteration.


What is a "const" ?

Its a way to declare an variable, they are like "let" declarations but, as their name implies, their value cannot be changed once they are bound. In other words, they have the same scoping rules as "let", but you can’t re-assign to them.