Just the glasses.
What? You thought you’d get to 2nd base after the first lesson? Let’s take it slow so you can concentrate.
Variables are common throughout every programming language. You should remember them from math class. Like x + y = 10, x and y are variables. Unless you were too busy hiding your boner during algebra class.
In any program you have to keep track of stuff. Like if we’re building a dating site, we’d need to keep track of my age, sexual preference, turn ons, favorite movies, and things like that. We put info like this into variables. A variable is just a container for a value. That value can vary, hence the name variable.
We’re going to start writing code in our script dot JS file. Let’s create a variable. Do this by writing the keyword var, then the name of our variable, which can be anything you want, but make it somewhat related to what it’s going to hold, we’ll do age, and then a semicolon. Ending statements and variable declarations with a semicolon isn’t required in javascript, but you should do it anyway. It will keep you organized and out of trouble, kind of like wearing protection.
Javascript is a loosely typed language. You can stick anything you want into a variable. Get your mind out of the gutter boys! It means you don’t have to specify whether the value will be an integer or string. In strongly typed languages like C and Java you have to specify the type of variable you are creating.
In javascript there are a few rules for variable names. They can contain letters, numbers, underscores and the the dollar sign. But, they can’t start with a number.
Now we need to assign a value to our variable like this, age equals 21. Which may or may not be my age. The shorter way to do it is just set the value when you declare the variable, like var age equals 21 all on the same line.
Javascript is case sensitive. So Age and age would be different variables.
You can declare multiple variables on one line with commas, instead of separate lines. Like var = age, sex, location. You can also set their values at the same time. For all you youngins out there, this is how we used to do things on AOL. (beat) Don’t stalk me bro.
Back to script dot js for an example. We have var A equals 350 and var B equals 70. Then we set a new variable called total to A plus B. The value of total would be set to 420.
We use console dot log to output this value to the console. Just write console dot log and then the variable ‘total’ in parentheses. Now go back to the html page and hit refresh. You’ll see that the value has been output to the console. Once you get some mad brogramming skills from codebabes lessons you’ll use console dot log all the time to debug code.
In programming the PLUS sign is called an operator. You can also use MINUS, star for multiplying and forward slash for dividing.
A lot of times, a variable appears twice in a statement, like if we do var problems equals 9, and then, problems equals problems plus 90. Then the new problems variable would be 99.
But there’s a shortcut. Problems plus equals 90 would mean the same thing. You can do this with all the other operators too, minus, plus, star, you get the idea.
If we want to increment a variable’s value by one there’s an even better shortcut, which is plus plus. Plus plus is equivalent to plus equals 1. In programming you use this to increment a value by one. Like when you're looping over an array. That sentence might sound like you’re taking crazy pills, but we’ll talk about loops and arrays later in this course.
Next we’ll talk about conditional statements, and introduce the IF statement. You know, like IF you make it to the next lesson, THEN clothing Item minus minus. If you know what I’m saying.
Questions or Comments?