programming basics lec 3-1

2
As soon as we write even the simplest program in any programming language, we have to keep track of pieces of information. If we are building a loan calculator, we need to keep track of the amount of the loan, the number of months, the interest rate. If we are writing a game program, we might need to know the current score, the position of the player on the screen, how many lives do we have left, what image do we use for our player. This is all data and we create variables to hold that data. Variables are simply containers. What we're doing is going out to the computer memory and grabbing a little piece of it, giving it a name to use while our program is running. We grab this space and we name it and then we put a value in it, like an email address or a date or a position or a number, and then we can change that value as we need to. Variables can vary, hence the name. So in JavaScript you create a variable like this. The word var written all lowercase, which is part of the JavaScript language, and then we name the variable. The name of the variable is up to us and it should represent the piece of data that we want to hold. So a variable called year, or one called customerEmail, or todaysDate, or while we are experimenting, just nonsense words like foo or even a single letter like x. Now the name that you use for your variable must be written as one word, there are no spaces allowed, and it can be made of letters, numbers, the dollar sign and the underscore. No other characters are allowed but you can't actually start with a number. So this variable name for example would not work, but reversing them so thenumber's at the end, that would work, and we will talk more about naming ourvariables later on when we talk about style guidelines, but I am just going touse simple names for now. All we are doing when we create a variable is carving out a little area of memory to hold a value. Right now after this line of code runs, this variable exists. It has a name, year, but it doesn't have a value. This is regarded as undefined and undefined has a special meaning in JavaScript. But there is no point to having a variable that stays undefined. So we can define or set the initial value of the variable when we create it. We could do that as two statements. I say var year, then year = 2011, and that puts the value in the variable. Now very important. The = sign here is setting the variable to the value 2011. It is not a polite description. It is a command. Put the value 2011 in the variable called year. You can also combine these into one statement, to both define and create the variable and to set its value. Now here is a place where JavaScript will let you be sloppy. I won't. Now technically the word var is not even required. In JavaScript if you just write a line of code like this

Upload: venkata-vikas

Post on 20-Jul-2016

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Programming Basics Lec 3-1

As soon as we write even the simplest program in any programming language, we have to keep track of pieces of information. If we are building a loan calculator, we need to keep track of the amount of the loan, the number of months, the interest rate. If we are writing a game program, we might need to know the current score, the position of the player on the screen, how many lives do we have left, what image do we use for our player. This is all data and we create variables to hold that data.Variables are simply containers. What we're doing is going out to the computer memory and grabbing a little piece of it, giving it a name to use while our program is running. We grab this space and we name it and then we put a value in it, like an email address or a date or a position or a number, and then we can change that value as we need to. Variables can vary, hence the name. So in JavaScript you create a variable like this.

The word var written all lowercase, which is part of the JavaScript language, and then we name the variable. The name of the variable is up to us and it should represent the piece of data that we want to hold. So a variable called year, or one called customerEmail, or todaysDate, or while we are experimenting, just nonsense words like foo or even a single letter like x. Now the name that you use for your variable must be written as one word, there are no spaces allowed, and it can be made of letters, numbers, the dollar sign and the underscore.

No other characters are allowed but you can't actually start with a number. So this variable name for example would not work, but reversing them so thenumber's at the end, that would work, and we will talk more about naming ourvariables later on when we talk about style guidelines, but I am just going touse simple names for now. All we are doing when we create a variable is carving out a little area of memory to hold a value. Right now after this line of code runs, this variable exists.It has a name, year, but it doesn't have a value. This is regarded as undefined and undefined has a special meaning in JavaScript. But there is no point to having a variable that stays undefined. So we can define or set the initial value of the variable when we create it. We could do that as two statements. I say var year, then year = 2011, and that puts the value in the variable. Now very important. The = sign here is setting the variable to the value 2011.It is not a polite description. It is a command. Put the value 2011 in the variable called year. You can also combine these into one statement, to both define and create the variable and to set its value. Now here is a place where JavaScript will let you be sloppy. I won't. Now technically the word var is not even required. In JavaScript if you just write a line of code like this without var, JavaScript will go looking for an existing variable called year to put this value in, but if it doesn't find it, it will just make it.

However, we are always going to use the word var when defining our variables.There are couple of situations where leaving var off can lead to unexpectedbehavior, so make a habit of always using it in your JavaScript. Now once again JavaScript is case sensitive. That means if I create a variable called x with a lowercase x, that's one. If I use the word uppercase X, that's two. These are two different variables. This second line here might have been an accident.Maybe I meant to say lowercase x, but because of the automatic creation ofvariables without the word var, you would now have two different variables andnothing in JavaScript would actually give you an error on this.So be careful when you're naming your variables. Now if you are creating multiple variables at the same time, you can of course do them on multiple lines like this but you can actually combine them on to

Page 2: Programming Basics Lec 3-1

one line, separating the variable names with commas. It's just a shorthand way of doing this. And similar to that if you're actually creating multiple variables and giving them all initial values you can still combine them on to one line. This makes it a little easier to read, a little shorter to write.

Now in JavaScript, once you've actually created a variable you can put anything in it. Numbers, text, dates, it could start with a number, then put some text, then put a date in it, and that might not sound unusual, but a lot of other languages don't let you do that. Creating a variable in many languages does not just mean defining a container but also saying what type of container it is and what it can hold, and while that's not essential in JavaScript, it's really useful to know. So we are going to take a quick look at that idea.