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.

No comments:

Post a Comment