Showing posts with label variable. Show all posts
Showing posts with label variable. Show all posts

Sunday, January 4, 2009

Variables are Fun!

So the book finally got around explaining variables and "var" on page 44.

variable: a storage location in memory with a unique name.
var: the JavaScript keyword used to create a variable.

It looks like the way to create a variable is to write
var variablename ;
* every variable must be unique and meaningful, says the book. Unique, obviously, because if you have two different variables named the same thing that will cause an error. Meaningful, because, let's face it, you're not going to remember what xijoije means. Programming is very organized - you don't have the luxury of letting your code get messy before taking a day to clean it up. Practically any programming book will beat you over the head about formatting and naming standards, and for good reason.

... so you want your variable to have an initial value? Yea, me too. Good thing the next page tells me how to do this. Apparently that's called initializing a variable. (Creative lingo here, huh?) The good news is that the code to do this is fairly obvious. You just add a = initial value and viola, you're set.
var variableName = Initial value;
JavaScript makes some things easy for us programming noobs. I just found out that it will assign a value for your variable automatically based on what initial value you give it. So if you say it's initial value is 300, for instance, it will know that it's a number. Sometimes JavaScript can't guess or guesses wrong and you have to go in and tweak it... but they don't want to tell me how to do that yet.

Chapter 1 Summary

In chapter one of Head First JavaScript I learned about alerts, events, and functions. I also learned some basic ingredients of interactivity - onclick and onload. I am unclear what "var" means (though I know it's important in the code) and am looking forward to learning this later in the text.

The next chapter is entitled "storing data" so it seems a little more important than how to annoy users with an automatic greeting on page load. I'll let you all know how it goes.

(edited to add: variable is explained on page 44)