Functions
- What is a function?
- Pre-defined functions
- Scope of Variables
- Function Expressions
- Anonymous Functions
- Callback Functions
Objects
- What is an object?
- Accessing Object Properties
- Calling an Object’s Methods
- Altering Properties/Methods
Array Properties and Methods
- The length property
- Stack Methods
- Queue Methods
- Reordering Methods
- Manipulation Methods
- Location Methods
- Iterative Methods
- Working with array of objects
DOM
- The Document Object Model
- Node Relationships
- Working with the Contents of Elements
- Getting Elements by ID, Tag Name, or Class
- Adding and removing element
Built-in Objects
Using Events in JavaScript
Handling Input and Output
Ajax and JSON
A queue adds items to the end of a list and retrieves items from the front of the list. Because the push() method adds items to the end of an array, all that is needed to emulate a queue is a method to retrieve the first item in the array. The array method for this is called shift(), which removes the first item in the array and returns it, decrementing the length of the array by one. Using shift() in combination with push() allows arrays to be used as queues:
var colors = []; //create an array
var count = colors.push("red", "green"); //push two items
alert(count); //2
count = colors.push("black"); //push another item on
alert(count); //3
var item = colors.shift(); //get the first item
alert(item); //"red"
alert(colors.length); //2
This example creates an array of three colors using the push() method. The shift() method being used to retrieve the first item in the array, which is “red”. With that item removed, “green” is moved into the first position and “black” is moved into the second, leaving the array with two items. JavaScript also provides an unshift() method for arrays. As the name indicates, unshift() does the opposite of shift() : it adds any number of items to the front of an array and returns the new array length. By using unshift() in combination with pop(), it’s possible to emulate a queue in the opposite direction, where new values are added to the front of the array and values are retrieved off the back, as in this example:
var colors = new Array(); //create an array//another way of creating array
var count = colors.unshift("red", "green"); //push two items
alert(count); //2
count = colors.unshift("black"); //push another item on
alert(count); //3
var item = colors.pop(); //get the first item
alert(item); //"green"
alert(colors.length); //2
In this code, an array is created and then populated by using unshift(). First “red” and “green” are added to the array, and then “black” is added, resulting in an order of “black”, “red”, “green”. When pop() is called, it removes the last item, “green”, and returns it.
Examples
Create an array of elements and add an element to the start of the array.
var numbers = [22,112,64,98,6];
numbers.unshift(11);
Create an array of elements and remove the first element of the array.
var numbers = [22,112,64,98,6];
numbers.shift();