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

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.

Gary

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.