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 The Mad Libs Challenge Revisited

Lia Seltene
Lia Seltene
2,641 Points

Why did he call it a string? I thought they were strictly within quotes?

From 0:51 - 1:18

Basically, we need to sandwich the number of questions between an opening bracket and the string, questions left.

Ok, so he uses the word 'string' the way I am used to here. To refer to characters within quotation marks.

Notice that I'm able to put a number inside a string. Not all computer programming languages let you do this. But JavaScript makes it easy to insert numbers into strings using the plus operator. Then I'll add this string to the prompt.

Whaaaat?! /overdramatic

Why does he refer to a "collection of characters within quotation marks" + a number, as a string? Or + a variable?

Are there two meanings of the word string? A collection of characters within quotation marks AND ..that (??) concatenated with a variable and/or number?

2 Answers

Stuart Wright
Stuart Wright
41,120 Points

If you have:

x = "hello" + 5;

x is now equal to the string "hello5". That's just the way Javascript is. In most languages this would cause an error mixing types like this, but Javascript tries to guess what you're trying to do and converts the 5 to a string and adds it. It's not good practice to do this in my opinion but it works.

Lia Seltene
Lia Seltene
2,641 Points

Ohh okay.

Is this only when combining types that it converts whatever into a string?

Like.... Does document.write(x) or 'printing' things to the browser demand the same conversion?

In JavaScript

a + b

if a or b is a string, then both are converted to string, and the result is a string. It's explained in the standard (step 7).