26 Result of number plus string in JavaScript

Last updated on July 13th, 2020 at 11:22 am.

26 Result of number plus string in JS - JavaScript Tutorial 2018


This video is taken from the full course that will teach you HTML, CSS, Programming concepts and Javascript .

Transcript :

Hi, and welcome back.

It’s Ricky.

And in this video, let’s see what happens if we have a number, a string and the ‘+’ operator.

So what you’re going to see in this video is what happens if I do add a string to a number.

So what will happen when you add, in JavaScript when you add a number and a string, the number will get converted to a string.

So like in this case, this is a string.

You can see this is a string because it is enclosed with these double quotations.

You can also use the single quotations.

So, if we add these two, this is a number and this is a string.

If you add them, you’re going to get ‘Case 39’.

Also, if you add this, this is a string because it has the double quotation and this is a number.

So if you add this and this, you will get, if you add these two, you’re going to get ‘100100’.

You’re going to get that result.

So what we have here is what is called ‘concatenation’.

This is joining.

So the ‘+’ operator is going to join the two.

It’s going to join the string and it’s going to join the number.

And that’s what we get.

So that’s called concatenation.

The ‘+’ operator.

If it has to operate between a string and a number, it will join the two.

And that’s called concatenation.

So if I come back to the browser, I have two variables.

Variable ‘y’ and variable ‘x’.

Variable ‘y’ has been declared as a string.

So this one is a string.

And this one is a number.

Now if I come down here and I add the two, ‘x+y’ and then I display the result, it’s going to give us a concatenation, that is joining the two.

If I remove this so that it becomes a number, and then I do the addition again, display, you’ll see now it gives us a result of ‘200’.

If the first one is a string, now let me use, let me use the single quotation this time around.

Let’s say ‘Number Two’ and then we try to add ‘x’ and ‘y’.

This is clearly a string and this is a number.

If I try to add the two and I display, you can see it gives us a concatenation of ‘100Number Two’.

So let me add some space there, then come down there, click ‘Display’ and you can see the result it gives us is ‘100 Number Two’.

So is that the same? What if we changed, what if we change this one to multiplication or even, what’s going to happen? So let me come back here.

So this is not true for other operators.

So in other operators, if you have a string and a number, like in this case this is a number, that’s a string.

It’s going to treat this as a number, and it’s going to give you back a result of the subtraction of the two.

And the same is here.

Over multiplication, even a division, any other operator.

If you’re using any of those arithmetic operators, we have multiplication, we have division, we have the modulus.

Which one else do we have? Whichever else you can think of.

The subtraction.

If you use those with this, it’s going to give you a result of the two, treating both as a number.

So for instance in this one, let me just clear out all of this.

In this one we have ‘x – y’.

So this one, what’s going to give us, is this is going to give us ‘not a number’.

So if I display this one, it’s going to give us ‘not a number’ because it tried to treat this as a number but it was not a number.

So that’s what happened.

But if I change this, it will actually try to convert whatever is in here to a number.

So if I come here and I say ‘200’.

So it will convert this into a number.

Although the entire thing is a string, when it’s doing the calculation, it’s going to convert the string value to a number.

So let me come here and display.

And if I display, it gives us ‘-100’.

That is ‘100’ minus ‘200’.

If we make it a multiplication, we’re going to get the same results of 200 times a 100.

If you make it a division, if you make it a division, and you display the result, it’s going to give you ‘0.5’.

If you make it a modulus, a modulus basically gives you the remainder.

If I display this, the modulus is actually ‘100’.

Let’s do a different example.

If we say ‘y%x’, ‘y’ modulus of ‘x’.

Let me remove all of this.

So ‘y%x’, if we display this one, it will give us ‘0’ because ‘y’, ‘200’ divided by ‘100’, ‘100’ goes into ‘200’ twice, and the remainder is ‘0’.

If this was something like ‘3’, display.

So the modulus is ‘2’, because ‘3’ will go here ‘66’, I think it will go there ‘66’ times, and there will be a remainder of ‘2’.

Three times ‘6’, ‘18’, ‘3’ times ‘6’, ‘18’, ‘198’.

So there will be a remainder of ‘2’.

So the modulus simply gives you a remainder.

So in this video, we have seen that a number plus a string will become a concatenation.

However, a number and a string with any other number, the string will be converted.

The interpreter will try to convert the string into a number so that it can do the operation.

And that is why this was converted to a number and the result you got back was ‘90’.

This was converted to a number and the result you got back was ‘500’.

And if whatever is within the string cannot be converted to a number, it will return to us ‘not a number’.

So if you have any questions, feel very free to let me know.

I will see you in the next video.

I’ll see you in the next video.

If you have any questions, let me know.

Click the button below to watch this full course:

Comment Here

Join this free course:

How to host multiple WordPress
websites on a VPS

Close me!