Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Working With Numbers Numbers and Strings

Monika Frankowska
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Monika Frankowska
Full Stack JavaScript Techdegree Graduate 20,279 Points

Does anyone knows why multiplication of strings works as it were numbers?

It is a little confusing when we have to convert strings to numbers to add them properly, but when I try to multiply the answer from prompt it work without any problem.

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! It's because the plus sign + isn't just an addition symbol. It's also the symbol we use for concatenation. To be clear, this is also true of other programming languages such as Java and C#. JavaScript is pretty smart sometimes and has what it calls "type inference". In many cases, it can deduce the type of data you want by the way you use it.

Take a moment and run this in your console:

let a = "2";
let b = "3";
let c = 4;
let d = 5;

console.log(a + b);
console.log(c + d);
//This is where it gets interesting
console.log(a + c);
console.log(d + b);

// Let's try some other mathematical operations
console.log(a * b);
console.log(b / c);
console.log(a - c);
console.log(c % b);

The output here will be:

  • 23
  • 9
  • 24
  • 53
  • 6
  • 0.75
  • -2
  • 1

Because a and b are both strings, the + sign means to JavaScript that it should concatenate those two strings and the result will be the string "2" concatenated with the string "3"... or 23. But when we add two numbers together as we did with c + d we get back 9 which is exactly what we'd expect.

It gets interesting when you try and add a number and a string together. This is where JavaScript makes a "best guess". Because you're adding a string with a number, it takes the number and turns it into a string and concatenates it with the other string.

As you can see, all the other mathematical operators work just as expected because it understands that you want a number there. But because the + sign is also used to manipulate strings, it has to make a guess, and it will change it to a string.

In short, if you know you want to do a mathematical operation on numbers, you should always make sure they are numbers first. You can end up with some really unexpected results otherwise.

Hope this clears things up! :sparkles: