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

Desmond Dallas
Desmond Dallas
6,985 Points

string

Can someone answer this for me please.

Why in the alert function why would you put (name + "Sam" + age); Why wouldnt you just put Sam is 26?

Var name = "Sam" var name = "26" alert(name + " Sam " + age);

1 Answer

Steven Parker
Steven Parker
229,744 Points

Perhaps a bigger question is if name = "Sam", why would you put name + "Sam"? Won't that be redundant?

But the reason you would put "Sam" + age" instead of "Sam is 26" is that it allows you to display a different age just by changing the value of the variable. I know it doesn't seem like a big deal in this simple example, but in more practical programming situations you will often need to display a variable instead of something permanent in a string.