Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


Gary Gordon
Gary Gordon
192 Points

I am new to programming and working through the "Basics".

I am new to programming and working through the "Basics."

My question is .. I was presented with the following:

alert("Hello " + prompt("What is your name?"));

I am confused as to WHY Javascript (in this case) doesn't first display the ALERT of "Hello" .. and then .. ask What is your name?

Since I'm new to programming, it would have made more sense to me that it would work in the a somewhat linear fashion and run the code as it goes through it. Meaning, it would see ALERT and pop up the alert box. Then, it would see "Hello" and just display this string in the alert box. And, then immediately after displaying "Hello" it would have then displayed a prompt with the question "What is your name?"

I'm just confused and was hoping someone could explain "exactly" why it gets written this way and not in a manner that appears more logical (from a newbie's perspective to programming). And if you could explain this without saying .. "that's just the way it works" .. that would be awesome. hahaha

I'm just trying to fully grasp WHY it gets written this way?

And .. is this similar in PHP (as it is in Javascript)?

Any details you can provide to explain this fully would be appreciated.


2 Answers

Expressions are executed from the inside out, especially when parentheses are involved. A mathematical example helps illustrate this: (5 * (3+2)) will equal 25 instead of 17.

This is a similar across the board in the world of programming.