Comp 110 Concatenation


Concatenations means, simply, to join one string to another. You can think of it as "adding" the strings together. When a program concatenates two strings, the result is a single string value.

For example: 

let cat: string = "Cat";
let dog: string = "Dog";
print(cat + dog);
/* CatDog would be printed. 
Notice there is no space between the two strings. */

To add a space between "Cat" and "Dog", you need to concatenate a string containing a space to the expression.

print(cat + " " + dog);
//Cat Dog would be printed.

 You can also concatenate numerical values - even if they aren't of type string - which can be very helpful!

A simple example:

let x: string = "COMP";
let y: number = 110;
let result: string = x + y;
/* Output: COMP110 
Note that the type result is of type string */

Another example of concatenation with multiple strings and numbers:

let getAge = (age: number): string => {     
    let result: string = "Hello! I am " + age + " years old.";     
    return result;
//Output: Hello! I am 13 years old.