You can recognize commands because they have parenthesis
Web Specific
alert()
document.write()
Universal to Javascript
isNaN() - checks to see if a particular value is a number or not
Types of Data
Numbers
document.write(5 + 15);
whole numbers, decimals, negative
String
can be in double or single quotes
"This isn’t fair" (us double quotes to enclose a string with a single quote inside it)
'He said, "Hello."' is a valid string (how to include quotes in a string)
"He said, "Hello.\ "" (use the \ - an escape character - to tell it to include that quote in the string)
'He said, "This isn\'t fair."' (you have to use the escape character to make that phrase work)
Boolean
you can use boolean value to check if a user did something or not, such as did they supply an email address - true or false
Variables
Stores information for later use
Naming Convention for Variables
Starts with a letter, $, _Contain alphanumeric characters and $ and _ (No spaces)
Case-sensitive
Don't use special keywords like alert, break, catch, for, new, this, void.
You can create multiple variables at once like this:
var playerScore, gameRound, playerName;
And you can declare and store multiple pieces of information inside like this:
var playerScore=52, gameRound=1, playerName='Bob';
Basic Math
+ - / *
var price= 5, itemsOrdered=2, totalCost=price*itemsOrdered;
make sure to use parenthesis when having multiple mathematical operations, for example:
(4 + 5) * 10 (this returns 90)
4 + 5 * 10 (this returns 54)
what ever in in the parenthesis happens first.
Combining Strings
Combining strings is called concatenation
var firstName = 'Bob';
var lastName = 'McPoopface';
var fullName = firstName + lastName;
This gives you BobMcPoopface
var firstName = 'Bob';
var lastName = 'McPoopface';
var fullName = firstName + ' ' + lastName;
This gives you Bob McPoopface (with a space)
Combing Numbers and Strings
Automatic Type Conversion
Whenever you use the + operator with a string value and a number it concatenates it to a string
var numOfPoops = 900;
var message='Your dog ate ' + 'numOfPoops' + 'poops today.';
returns: Your dog ate 900 poops today.
Sometimes converting is bad.
For example:
var numOfCatPoops = 25;
var numOfDogPoops = 5;
var numOfPoops = numOfCatPoops + numOfDogPoops;
var message='Your dog ate ' + 'numOfPoops' + 'poops today.';
returns: Your dog ate 255 poops today. (instead of 30)
To prevent this:
(Two ways to convert)
1. Add + to the beginning of the string with the number:
var numTotal = +numStuff
2. Number(variable)
var numTotal = Number(numStuff) + numMoreStuff
Changing the Values in Variables
var score = 0;
score = 100;
var score = 0;
score = score + 100;
Add 100 to what ever value is currently store in "score".
You are preforming a mathematical operation on the value of a variable and then storing that value back into the variable.
Shortcuts:
score += 10;
score = score + 10;
score -= 10;
score = score - 10;
+= -= *= /=
You can also use += for strings:
Adds or subtracts 1
score++
score--
++ --
Arrays
You can store more than one value in a single place, so you don't have to keep creating tons of variables.
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May'];
var listOfStuff = [];
an empty array that can be supplied with information later ( [] two brackets)
Inside of an Array you can store:
Numbers
Strings
Boolean
Arrays
Other Objects
(you can store any combination of from this list)
Getting Items Out of the Array
same as ActionScript, the items have position numbers/index numbers
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'July'];
alert(months[2]);
this would return Mar
Change the Value of an Item
months[0] = 'January';
now Jan is changed to January
Get the Last Position of the Array
months[months.length-1];
gets the total number of positions and subtracts 1
so, for months this would be 6 (since I have only listed 7 months)
When working with loops you might want:
var i = 0;
alert(months[i]);
Adding Items to the End of an Array
var properties = ['red', '14px', 'Arial'];
Three ways:
properties[3] = 'bold';
properties[properties.length] = 'bold';
properties.push('bold');
Adding Items to the Beginning of an Array
var properties = ['red', '14px', 'Arial'];
properties.unshift('bold');
you can push and unshift multiple values like this:
properties.push('bold', 'italic', 'underlined');
properties.unshift('bold', 'italic', 'underlined');
push() and unshift() are methods
they return a value, the total items in the array
for example:
var p = [0,1,2,3];
var totalItems = p.push(4,5);
totalItems is 6
Deleting Items from an Array
Similar to Action Script
pop();
shift();
var cats = [Mr.Whiskers, Ugly Cat, Snowball];
var catDeath = cats.pop();
the value catDeath is Snowball
and cats now has [Mr.Whiskers, Ugly Cat]
Adding and Deleting with splice()
this adds and deletes item from the interior positions of the array
var cats = [Mr.Whiskers, Ugly Cat, Snowball, Prof.Muffintop];
cats.splice (1,2);
The 1 is the index number
The 2 is the number of positions to delete
var cats = [Mr.Whiskers,Ugly Cat, Snowball];
Only Mr.Whiskers and Prof.Muffintop remains!
var catsAlive = cats.splice (1,0, 'Snowball Jr., Son of Ugly Cat', "Lil' Kitty Winkins");
var cats = [Mr.Whiskers, Snowball Jr., Son of Ugly Cat, Lil' Kitty Winkins, Prof. Muffintop];
0 indicate you are deleting 0 items
3 new cats are born, now catsAlive has 5 cats!
The Circle of Life, it moves us all.
Replacing Items with splice();
add and delete at the same time
var cats = [Mr.Whiskers, Ugly Cat, Snowball, Prof.Muffintop];
var catsAlive = cats.splice (1,2, 'Snowball Jr., Son of Ugly Cat', "Lil' Kitty Winkins");
var cats = [Mr.Whiskers, Snowball Jr., Son of Ugly Cat, Lil' Kitty Winkins, Prof. Muffintop];
---------------------------------------------------------------------------------------------------------------------------
TUTORIALS
document.write(' <p>');
you can write html into the page
prompt('Tell me your name or I'll doing a flying kick to your face');
this produces a dialogue box similar to alert, but you retrieve an answer
Add 100 to what ever value is currently store in "score".
You are preforming a mathematical operation on the value of a variable and then storing that value back into the variable.
Shortcuts:
score += 10;
score = score + 10;
score -= 10;
score = score - 10;
+= -= *= /=
You can also use += for strings:
var name = 'Bilbo';
var message = 'Hello';
message = message + ' ' + name; >>becomes>> message += ' ' + name;
Adds or subtracts 1
score++
score--
++ --
Arrays
You can store more than one value in a single place, so you don't have to keep creating tons of variables.
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May'];
var listOfStuff = [];
an empty array that can be supplied with information later ( [] two brackets)
Inside of an Array you can store:
Numbers
Strings
Boolean
Arrays
Other Objects
(you can store any combination of from this list)
Getting Items Out of the Array
same as ActionScript, the items have position numbers/index numbers
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'July'];
alert(months[2]);
this would return Mar
Change the Value of an Item
months[0] = 'January';
now Jan is changed to January
Get the Last Position of the Array
months[months.length-1];
gets the total number of positions and subtracts 1
so, for months this would be 6 (since I have only listed 7 months)
When working with loops you might want:
var i = 0;
alert(months[i]);
Adding Items to the End of an Array
var properties = ['red', '14px', 'Arial'];
Three ways:
properties[3] = 'bold';
properties[properties.length] = 'bold';
properties.push('bold');
Adding Items to the Beginning of an Array
var properties = ['red', '14px', 'Arial'];
properties.unshift('bold');
properties.push('bold', 'italic', 'underlined');
properties.unshift('bold', 'italic', 'underlined');
push() and unshift() are methods
they return a value, the total items in the array
for example:
var p = [0,1,2,3];
var totalItems = p.push(4,5);
totalItems is 6
Creating a Queue / FIFO
playlist.push('Dumb Song');
nowPlaying = playlist.shift();
This cycles through, you add a song to the end of the list.
Then you play the first song on the list, by removing it and storing it inside nowPlaying. Deleting Items from an Array
Similar to Action Script
pop();
shift();
var cats = [Mr.Whiskers, Ugly Cat, Snowball];
var catDeath = cats.pop();
the value catDeath is Snowball
and cats now has [Mr.Whiskers, Ugly Cat]
Adding and Deleting with splice()
this adds and deletes item from the interior positions of the array
var cats = [Mr.Whiskers, Ugly Cat, Snowball, Prof.Muffintop];
cats.splice (1,2);
The 1 is the index number
The 2 is the number of positions to delete
var cats = [Mr.Whiskers,
Only Mr.Whiskers and Prof.Muffintop remains!
var catsAlive = cats.splice (1,0, 'Snowball Jr., Son of Ugly Cat', "Lil' Kitty Winkins");
var cats = [Mr.Whiskers, Snowball Jr., Son of Ugly Cat, Lil' Kitty Winkins, Prof. Muffintop];
0 indicate you are deleting 0 items
3 new cats are born, now catsAlive has 5 cats!
The Circle of Life, it moves us all.
Replacing Items with splice();
add and delete at the same time
var cats = [Mr.Whiskers, Ugly Cat, Snowball, Prof.Muffintop];
var catsAlive = cats.splice (1,2, 'Snowball Jr., Son of Ugly Cat', "Lil' Kitty Winkins");
var cats = [Mr.Whiskers, Snowball Jr., Son of Ugly Cat, Lil' Kitty Winkins, Prof. Muffintop];
---------------------------------------------------------------------------------------------------------------------------
TUTORIALS
document.write(' <p>');
you can write html into the page
prompt('Tell me your name or I'll doing a flying kick to your face');
this produces a dialogue box similar to alert, but you retrieve an answer