Comp 110 Concatenation

Concatenation

Concatenation 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 = "Cat";
let dog = "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);
//The string: '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 = "COMP";
let y = 110;
let result = x + y;
print(result);
/* Output: 'COMP110' Note that the type result is of type string...the concatenated number is used as a string*/