Let's talk about string operators.
Let's get back to Visual Studio code and let's get started.
I'm going to copy this command and down here paste that and I will change this one to string operators.
In JavaScript, we have one primary string operator and that is the plus operator.
So here I'm going to put it inside bracket and start.
And this one is used to combine two or more strings together and this is what we call concatenation.
Let's have some case study here and I'm going to paste it as that.
Let's consider a case where you want to create a personalized greeting message by combining a fixed
greeting string with a variable name string.
So here I'm going to declare a variable called greeting and it is equal to Hello.
And our at comma and space here.
When I save it prettier have formatted by adding double quotes around it.
Then I'm going to declare a variable as first name.
And is equal to Thomas.
And I'm going to combine these strings together.
And I'm going to declare a variable called message and is equal to greeting plus first name.
If we console log the message we have.
Hello from us and I can combine two or more strings together.
I can add last name.
Is equal to.
She nybrua And I'm going to add to this.
So it's going to be greetings plus.
The last name.
Save it and let's have a look at the message.
And we have Thomas.
But as you can see, we don't have spacing.
So to be able to add space, what we can do is that we can bring it here space, save it, and let's
check it out here.
And indeed we have some spacing or we can avoid this spacing.
And inside here we are going to add some spacing here.
So it's going to be called space and we add the plus.
This is what we call the concatenation and we have a new feature called template literal.
So instead of using this plus plus, we have a better way of doing it.
But don't worry, as you move on, you will learn more about ins and outs of JavaScript.
So if I save it and check it out here, we have the same result.
Next step is we can use the plus equal to.
So here I'm going to make use of the plus equal to string operator.
And this is often used with strings to append or concatenate strings to the end of existing string.
Let me show you an example here.
Let's say we have a sentence which says, I love you.
And if I want to add some string at the end, remember the increment and decrement.
That is the data types.
Do you remember under the arithmetic operators we can use plus equal sign and we can use the same on
strings.
So here I will bring in the sentence plus equal to let me add space here plus equal to.
Then I will add JavaScript.
With this one.
What will be the result if I console.log sentence is going to be I love JavaScript, so let's check
it out here a sentence and indeed I love JavaScript.
So how can we add some spacing here?
We can add it here or we can remove that and over here we are going to add some spacing and it's going
to be as code code here and space and space here.
And I'm going to add plus here.
With this one.
Let's see the answer.
And we have the formatted string as that.
But take note about this, which is really important.
When using the plus operator with a number and a string JavaScript will treat the number as a string
and concatenate them together.
Let me show you an example here.
Let's declare a variable called value or some value, and I'm going to add string here and a number
I will say there are and then I will add ten at the end.
What will be the result is going to be there are ten.
With this one if I check the value.
And as you can see, all of them have been converted to a string.
And if I change this 1 to 10.
We are trying to add ten plus ten, but this is a string, so it's going to concat.
So let's check it out.
If we check the answer, we have 1010.
And you should be mindful about this situation.
So to be able to make sure that we are adding numbers together, we have a couple of methods on numbers
or strings.
But don't worry, as you move on, we are going to explore more.
Great.
So guys, these are examples of operators in JavaScript.