Created
November 11, 2014 10:43
-
-
Save ryan-w-moore/8591ca122bab867ed099 to your computer and use it in GitHub Desktop.
ARRAYS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var singleValue; | |
singleValue = 99; | |
var multipleValues = []; | |
multipleValues[0] = 50; | |
multipleValues[1] = 60; | |
mulitpleValues[2] = "Mouse"; | |
console.log(multipleValues[2]); // Mouse | |
// There is a short-hand | |
var multipleValues = [ 50,60,"Mouse" ]; | |
// [0][1] [2] | |
var multipleValues = []; // an empty array | |
var multipleValues = new Array(); // Arrays are objects | |
var multipleValues = Array(); | |
// Create an array with a size: | |
var multipleValues = Array(5); | |
// II. ARRAY PROPERTIES: | |
var multipleValues = [10,20,30,40,50]; | |
// Array's have properties, like the .length property | |
console.log(multipleValues.length); // Length = 5. | |
// III. ARRAY METHODS: | |
someFuntion(params)l // to call a function | |
//methods are functions that belond to an object | |
someObject.someMethod(); // to call a method | |
var multipleValues = [10,20,30,40,50]; | |
var reversedValues = multipleValues.reverse(); | |
.join(); | |
.sort(); | |
console.log( reversedValues.join() ); // "50,40,30,20,10" | |
// IV. ARRAYS ARE EVERYWHERE: | |
// A Real-World Example: | |
// There may come a time when you want to write a line of javascript that will say: | |
// "I want to know how many anchor tags exist on the page, how many links do I have" | |
// or how many h3's do I have? | |
// or how many img's do I have? | |
// paragraphs? | |
// Etc... | |
var myArrayOfLinks = document.getElementsByTagName("a"); // How many anchor tags on the page? | |
// RETURNS: | |
/* myArrayOfLinks | |
[0] <a href="somepage... | |
[1] <a href="otherpage... | |
[2] <a href="http://... | |
[3] <a href="http://... | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment