Please Buy now at Leanpub
Written and illustrated by GetContented.
Published on 2016-05-22.
This chapter is where you start to become a beginner programmer by learning how to read your first few simple programs.
We’ll open the Chrome web-browser now, because it’s easier to start programming in. If you don’t have it, visit https://www.google.com/chrome/ and follow the instructions there.
Open a new window with
File... New Window, then a console with
Below you’ll see your first program — just one line. Type it into the console carefully and press return to run it. If you make a mistake, try again on the next line, or start again.
When you pressed return, a dialog box appeared, so-named because the computer is “talking” to us, and needs a response. We give one by clicking the button on that window which will dismiss it, and it will disappear. This tells the computer you’ve seen the window.
After it’s gone, you’ll see undefined is “printed” to the console. This is normal, but for now ignore this.
For simplicity, our first program is only one statement. We’ll see programs with more soon.
alert. A function is a kind of block (or parcel) of code; it’s like a mini-program, in a way!
When it gets to the round brackets written after alert, which are called parentheses, it sees them and that’s how it knows we’re telling it to call that function. We’ll explain that next.
Calling a function means executing all of the statements inside the definition of that function, in sequence (one after another): evaluating its contents and responding with what’s called a return value. This is the value that a function call can give back as its “answer” to being called. This will become clearer as we keep going.
100 or the word
alert function responds with the
undefined value. Another way to say this is that the return value of
alert is the value
undefined value is returned when the computer hasn’t got anything more meaningful to respond. It’s returned here because all function calls must return some value.
If you think about what an alert message does, it’s whole job is just to put a message on the screen. It doesn’t have anything meaningful to feed back into the computer. That’s why we get the
undefined value back from it.
Putting a blank alert on the screen is ok, but it’d probably be more useful and interesting if we could actually display a text message.
Let’s call the alert function with a message now:
If you run this program (by typing it into the console, then pressing the return key), an alert will pop up with the message in it. As before when you dismiss it undefined is printed in the console.
We can tell from doing this that the message shown is whatever value we put in quotes in-between the parentheses (the rounded brackets).
What about if we write two statements on the one line:
alert("hello") ; alert("hello, again") ;
If you try this, you’ll see that it does the left statement first, then the statement on the right, displaying an alert each time and waiting for you to dismiss it: it runs them in sequence.
Well we made it to the end of the first lesson. Well done! Take a breather, and then try the homework.
If you’ve enjoyed reading this, please consider purchasing a copy at Leanpub today
Please follow us, and check out our videos Follow @HappyLearnTutes