JavaScript Tutorial Part 2, Variables.

April 16th, 2014


Variables store values such as text, numbers, or objects. Variables are either declared and then later set, or can be set when they are declared.

var myNumber; //declaring but not setting
var myNumber = 93;//setting when you declare

You can set variables like so:

var myNumber = 1;

or

var welcomeMessage = 'This is a string';

Variables can also be HTML objects by using document.getElementById.

var header = document.getElementById("header");

Where the part in quotes is the id of the object you want to access.

You can set part of your HTML to a variable’s value by using the document.innerHTML command.

document.getElementById('myId').innerHTML="joeybabcock.me";

(note this uses getElementById, which we will get into better detail later)

In addition, variables can even be other variables combined.

var y = 1;
var z = 2;
var x = y + z;

x would equal 3.

Click Here for a more in depth tutorial of innerHTML and getElementById, in Part 3.


Comments

Comments Are Not Yet Implemented.