array - is a data structure that is used to store a collection of ordered data. Like the key-value pairs in object but a kind of object where in the key is numbered and sorted. And its value can be of any data type. The key always starts with 0. For example the empty array below. The first key should be array[0]. You can assign any value to the array[0].
Empty array:
Syntax 1:
let array1 = new Array();
Syntax 2:
let array2 = [];
=================
let yourArray = new Array();
yourArray[0] = 200;
yourArray[1] = 100;
yourArray[2] = 500;
let answer = 0;
function getTheSum(){
for(let elemX of yourArray){
answer += elemX;
}
document.writeln('sum: ${answer}');
}
=================
HTML
getTheSum()
Result:
sum: 800
Note: If you notice the for-each loop that was used is different. It uses for..of instead of for..in. Both for-each loop functions the same. The only difference is for..in iterates not just in numbers. So, it is kind of slow compare to for..of.
Deque - the data structure use in javascript array. The methods used are pop(), push(), shift(), and unshift(). The pop() and push() is last in first out (LIFO) while the shift() and unshift() is first in and first out (FIFO). You can use all this methods to either add or delete an element though it is advisable to use the pop() and push() since it is faster than the other two methods. Also, never attempt to decrease the length of an array for it will erase some of the elements. For example in the above code, if you set yourArray.length = 0; Everything will be deleted.
==================
let yourArray = new Array();
yourArray[0] = 200;
yourArray[1] = 100;
yourArray[2] = 500;
let answer = 0;
yourArray.push(1000);
yourArray.push(600, 500, 400);
yourArray.shift()
function getTheSum(){
for(let elemX of yourArray){
answer += elemX;
}
document.writeln('sum: ${answer}');
}
=================
HTML
Result:
sum: 3100
Note:
1. Your array was empty at first.
2, Then you add using bracket notation. Your array was [200, 100, 500].
3. Then you used push four times [200, 100, 500, 1000, 600, 500, 400].
4. Then you used shift one time [100, 500, 1000, 600, 500, 400]. The 200 was gone.
Multi dimensional arrays is allowed. Only the toString() method is supported by arrays. Never used the equality == to compare arrays since javascript has lots of automatic conversions being done.
YOU ARE READING
Javascript Programming
RandomI am just documenting things what i learned online in here. I do express things in my own words. And I create my own program samples which serves as my practice. I do this so that when someday I would need some notes to review, I can just visit in h...
