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

Garrett Miller
Garrett Miller
4,975 Points

Did not parsefloat or parseInt my prompt and still allowed me to multiply the string.

Wrote it on the fly and there are no errors. Is anyone able to explain why I can multiply a string here?

const endNum = prompt('please enter a number.');

let main = document.querySelector('main');

main.innerHTML = Math.floor(Math.random() * endNum) +1;

//check type console.log(typeof endNum); console.log(typeof (Math.random() * endNum));

1 Answer

Steven Parker
Steven Parker
231,236 Points

JavaScript has a mechanism known as type coercion that implicitly converts values from one data type to another when needed to satisfy the requirements of an operator.

For more details, see the MDN page on Type Coercion.