Welcome

Hey there, welcome to my little space on the net.

Tuesday, August 5, 2014

programming with javascript - lesson two - variables


Variables

Variables are where we store information for our program to interact with.  They're kind of like words and you get to define what they mean!

Here are the types of variables that exist in Javascript (JS) and how we can make them

Ignore the console.log() stuff for now.. it's basically a way for you to see what is in your variable and to see what the program thinks is in that variable.

String
strings store text.. so any number of numbers, characters and special characters.  So stuff like aA$d!13. Strings have to start and end with either ' or ".  No mixing allowed.

var thisIsAString = 'hello'
var thisIsAlsoAString = "123hello"
var evenThisIsAString = '!@#$h21i(*'
console.log( thisIsAString )
console.log( thisIsAlsoAString )
console.log( evenThisIsAString )

Integer
this stores whole numbers so like 1, 10, 12, -13, 0 (negative or positive are ok)

var thisIsAnInteger = 10
var thisIsAlsoAnInteger = -100
var evenThisIsAnInteger = 0
console.log( thisIsAnInteger )
console.log( thisIsAlsoAnInteger )
console.log( evenThisIsAnInteger )

Number
why do we need a number type? because numbers can store fractions

var thisIsANumber = 10.01
console.log( thisIsANumber )

Boolean
a fancy word for true or false

var thisIsABoolean = true
var thisIsAlsoABoolean = false
console.log( thisIsABoolean )
console.log( thisIsAlsoABoolean )

Array
these are basically collections of variables

var thisIsAnArray = [ 0, 1, 2 ]
var thisIsAlsoAnArray = [ 0, 'abc', 19.2 ]
console.log( thisIsAnArray )
console.log( thisIsAlsoAnArray )

Function
these guys are basically ways for us to organize our program.  I guess it'd be.. like a paragraph.

var thisIsAFunction = function(){

}
console.log( thisIsAFunction )

Object
in addition to their other types.. all variable types in JS are, at their core.. objects.  It's kind of like how a word can be an noun, but it's still a word

var thisIsAnObject = {}
var thisIsAlsoAnObject = new Object();
console.log( thisIsAlsoAnObject  )

Special Types

undefined
if we've never defined our variable word, then when we try to reference that word it is undefined.. you'll be seeing this in your console if you use it to do all the stuff from above.. but I'll explain why much later
var thisIsUndefined
console.log( thisIsUndefined )

null
traditionally.. this just means that there's nothing in there..
var thisIsNull = null
console.log( thisIsNull )

Breaking it down

The "var" is a special word that tells the program to create a variable and the text right after that is the variable's name.  The equal sign is an assignment and the text to the far right is the value.

so.. to create a variable
var (variable's name) = (variable's value)

Rules

For simplicity's sake, let's restrict all variable names to upper and lower case characters, and the underscore "_".

What're we taking away?

Congrats! You've learned how to store data in a program.  Data that you can then manipulate to do.. whatever it is you want to do.  Next time, I'll cover some operators so that you can do something with your data

No comments:

Post a Comment