Skip to main content

More about Strings!

Now that you've learnt about Data Types have you ever thought about how can i join different data types to form a string?

Here's an example:

const fullName: string = 'Faith Ye';
const age: number = 16;

How can we form a string with the value 'She is Faith Ye and her age is 16'

String Concatenating

One way to do is is by String Concatenating

const fullName: string = 'Faith';
const age: number = 16;

const result = 'She is ' + fullName + ' and her age is ' + age;

However when using String Concatenation, one needs to take note of the spacings in between the variables and also it seems like there's much more code.

Alternativelty you can you ✨ Template Literals! ✨

Template Literals

const fullName: string = 'Faith';
const age: number = 16;

const result = `She is ${fullName} and her age is ${age}`;

Look at how much simpler it looks! To note that for template literals we are using String Literals (``) versus Single Quote ('') and Double Quote ("")