filmov
tv
49 Interpolation of strings JS - Javascript Tutorial 2018

Показать описание
The Full #Javascript #tutorial for #beginners :
Learn HTML, CSS and JavaScript
Javascript Tutorial 2018
49 Interpolation of strings - template literals - JavaScript for Beginners.
This is a Javascript tutorial from scratch for any beginner. Use the above link to watch the full JS video tutorial..
So in the last video, we looked at multi-line strings.
And in this video, let's look at how you can do interpolation using template literals in ES 2015.
So, let's say you have two variables, right.
And maybe you want to do some kind of arithmetic operation.
And while you're doing that operation, you want to add those numbers and use them within some statement.
So maybe you want to say ‘My sum is ’ and then you want to put the number there.
The result of these two, these two variables.
So if you wanted to do this in old JavaScript before interpolation was introduced, you’d have to, first of all you do, you write your string in there and then you add the ‘+’ which is used for joining the two.
And then you do your operation using your variables in there.
And then you do another ‘+’ and if there's anything else that you want write you’d write it there.
And that is the same thing that we have here.
So you see our, our string ends there.
Then you concatenate the sum of the two numbers.
We join the sum of the two numbers.
Then we want to join that and we also want to put this in a new line.
If you see this, this means in a new line.
So, it will take this next thing into a new line.
So, multiplying the numbers gives.
Then again you do another concatenation here, another concatenation.
Then you join that with a final ‘+’.
So you can see, this is a lot of work.
This involves a lot more code.
And, you can do the same thing using interpolation in JavaScript.
And in JavaScript ES 2015, the only thing you need to do is, you just write everything, make sure it's in a back tick.
You can see here, these are back ticks.
The one that we have here, that’s a back tick.
That’s also a back tick.
If you use anything else, it will not work.
So you use the back tick.
Then adding the numbers gives, then you see this.
The dollar sign, the dollar sign and these curly braces will do the operation which is there.
So this will be passed into a function and the function will do an operation of this and then put the result there.
So if you’re doing the number, the addition of these two numbers, it will add this number and then add this number and once it has added these two numbers, it will put the results there.
So it will replace the result with that, what is there.
And then, it will continue on writing all these and then it will also do the calculation of this.
And once it has done the calculation of this, it will put the result there.
So you see in this case you don’t have to do any concatenation the way we did here.
So this is of course best done as an example.
So let me go to the browser and see how we can do this as an example.
So, let’s say we have a var num1=20; and then we have var num2=11; So, you can just use these as variables.
Let’s say you want to, you want to add them.
If I do this, just add 2 back ticks, I can say ‘Num1 value is ‘, then I’ll use the dollar sign, curly braces and ‘num1’.
So this will replace the value of num1 with what is there.
So num1 and make sure you’re using back ticks for this.
If you use the single quotes, it’s not going to work.
So, if I display that, you see it tells us ‘Num1 value is 20’.
And also if I want to do num2, I can just say ‘Num2 value is ‘, display.
This is how interpolation will work.
But let’s say we wanted to do the same thing in the old, in the old JavaScript.
Currently the way it is in JavaScript, I’d have to do this.
So I’d have to do this.
“Num2 value is“, then I do, you know it, concatenation, and then, the variable num2.
So this, if I display, it gives us the same result and let me add a space there.
So if I add a space and display, you see what it tells us.
Num2 is 11.
So you can see this is an easier way for you to just do the result pretty quick.
You can even do a calculation in here.
So let’s say ‘Num2 Plus Num1 value is ‘.
So we can just do num2 + num1.
GET SOCIAL WITH ME:
Learn HTML, CSS and JavaScript
Javascript Tutorial 2018
49 Interpolation of strings - template literals - JavaScript for Beginners.
This is a Javascript tutorial from scratch for any beginner. Use the above link to watch the full JS video tutorial..
So in the last video, we looked at multi-line strings.
And in this video, let's look at how you can do interpolation using template literals in ES 2015.
So, let's say you have two variables, right.
And maybe you want to do some kind of arithmetic operation.
And while you're doing that operation, you want to add those numbers and use them within some statement.
So maybe you want to say ‘My sum is ’ and then you want to put the number there.
The result of these two, these two variables.
So if you wanted to do this in old JavaScript before interpolation was introduced, you’d have to, first of all you do, you write your string in there and then you add the ‘+’ which is used for joining the two.
And then you do your operation using your variables in there.
And then you do another ‘+’ and if there's anything else that you want write you’d write it there.
And that is the same thing that we have here.
So you see our, our string ends there.
Then you concatenate the sum of the two numbers.
We join the sum of the two numbers.
Then we want to join that and we also want to put this in a new line.
If you see this, this means in a new line.
So, it will take this next thing into a new line.
So, multiplying the numbers gives.
Then again you do another concatenation here, another concatenation.
Then you join that with a final ‘+’.
So you can see, this is a lot of work.
This involves a lot more code.
And, you can do the same thing using interpolation in JavaScript.
And in JavaScript ES 2015, the only thing you need to do is, you just write everything, make sure it's in a back tick.
You can see here, these are back ticks.
The one that we have here, that’s a back tick.
That’s also a back tick.
If you use anything else, it will not work.
So you use the back tick.
Then adding the numbers gives, then you see this.
The dollar sign, the dollar sign and these curly braces will do the operation which is there.
So this will be passed into a function and the function will do an operation of this and then put the result there.
So if you’re doing the number, the addition of these two numbers, it will add this number and then add this number and once it has added these two numbers, it will put the results there.
So it will replace the result with that, what is there.
And then, it will continue on writing all these and then it will also do the calculation of this.
And once it has done the calculation of this, it will put the result there.
So you see in this case you don’t have to do any concatenation the way we did here.
So this is of course best done as an example.
So let me go to the browser and see how we can do this as an example.
So, let’s say we have a var num1=20; and then we have var num2=11; So, you can just use these as variables.
Let’s say you want to, you want to add them.
If I do this, just add 2 back ticks, I can say ‘Num1 value is ‘, then I’ll use the dollar sign, curly braces and ‘num1’.
So this will replace the value of num1 with what is there.
So num1 and make sure you’re using back ticks for this.
If you use the single quotes, it’s not going to work.
So, if I display that, you see it tells us ‘Num1 value is 20’.
And also if I want to do num2, I can just say ‘Num2 value is ‘, display.
This is how interpolation will work.
But let’s say we wanted to do the same thing in the old, in the old JavaScript.
Currently the way it is in JavaScript, I’d have to do this.
So I’d have to do this.
“Num2 value is“, then I do, you know it, concatenation, and then, the variable num2.
So this, if I display, it gives us the same result and let me add a space there.
So if I add a space and display, you see what it tells us.
Num2 is 11.
So you can see this is an easier way for you to just do the result pretty quick.
You can even do a calculation in here.
So let’s say ‘Num2 Plus Num1 value is ‘.
So we can just do num2 + num1.
GET SOCIAL WITH ME: