Data Operations in TypeScript
TypeScript, Unlike other strongly typed languages, it allows you to perform operations on different data types without converting them into a single common type.
Let's look at an example of what is meant. Let's say we have a string and a numeric variable:
let test1: number = 123;
let test2: string = 'abc';
Let's perform the addition of these variables:
console.log(test1 + test2); // will bring out '123abc'
That is, you can add, for example, strings and numbers, and this will not lead to an error. That is, TypeScript only monitors that the programmer does not change the data type of the variable.
For example, in the following code we will try to write a string to a numeric variable and this will already lead to an error:
let test1: number = 123;
let test2: string = 'abc';
let test3: number;
test3 = test1 + test2; // trying to write a line
Without running the code, determine what the result of executing the code will be:
let test1: number = 123;
let test2: number = 456;
console.log(test1 + test2);
Without running the code, determine what the result of executing the code will be:
let test1: string = '123';
let test2: string = '456';
console.log(test1 + test2);
Without running the code, determine what the result of executing the code will be:
let test1: string = '123';
let test2: string = '456';
let test3: number = test1 + test2;
console.log(test3);
Without running the code, determine what the result of executing the code will be:
let test1: number = 123;
let test2: number = 456;
let test3: string = test1 + test2;
console.log(test3);
Without running the code, determine what the result of executing the code will be:
let test1: number = 123;
let test2: number = 456;
let test3: string = test1 + ' ' + test2;
console.log(test3);
Without running the code, determine what the result of executing the code will be:
let test1: number = 123;
let test2: number = 456;
let test3: string = '!';
let test4: string = test1 + test2 + test3;
console.log(test4);
Without running the code, determine what the result of executing the code will be:
let test1: number = '123';
let test2: number = '456';
console.log(test1 + test2);
Without running the code, determine what the result of executing the code will be:
let test1: number = +'123';
let test2: number = +'456';
console.log(test1 + test2);
Without running the code, determine what the result of executing the code will be:
let test: string = '1';
test += 1;
console.log(test);