Stop piffle about past and move to present. Topic for today are arrays. They are more advanced so we rather dive in immediately.
Array begin and end with square brackets and the values (data or information) are inside divided by commas. The last value is without comma. This also applies if you want to store only one value – no comma after it. In arrays, you can store whatever data types you want. It can be numbers, strings, boolean, another array, objects or mix of them. Few examples of basic arrays …
// accessing values var array3 = [“Python”, 1158, false, 'PHP']; array3; // return false array3; // return “Python” array3; // does NOT work – will cause error
– note: use console.log() or alert() methods to see the data:
When you store another array in an array, it is called multidimensional array or n-dimensional array according to number of arrays stored inside. For example, array with two arrays stored inside is two dimensional array, with five arrays is called five dimensional array and so on. In order to store an array inside another, create new variable and inside square brackets (indicating arrays) write another square brackets – these brackets mark new array. Inside these second brackets you can, again, write whatever data type you want. Just like with other data types, nested arrays are divided by commas. Some examples to know how these arrays look.
// basic one dimensional array var oneDimensionalOne = ; var oneDimensionalTwo = [true, “Eich”]; // Two dimensional array var twoDimensionalOne = [[13, false, 'gather'],[true, “design”, “Vignelli”]]; var twoDimensionalTwo = [[1, 2, 3],[2, 4, 6]];
Accessing values in multidimensional array is similar to one dimensional. Write name of the array followed by square brackets. However, in case of multidimensional array you have to use two or more indexes, so two or more brackets. First index indicate the position of the nested array and the second, for two dimensional array, indicate the value you want to access. Example:
var twoDimensionalOne = [[13, false, 'gather'],[true, “rating”]]; var twoDimensionalTwo = [[1, 3],[12, false]]; twoDimensionalOne; // will return true – 1 = second value in “parrent array”, 0 = first value in nested array twoDimensionalTwo; // will return 1 twoDimensionalOne; // return 'gather' twoDimensionalTwo; // will return false
As you could notice, nested arrays are independent so they don’t have to store same number of values or elements. You can store one value in one and thousand in second if you want.
var array1 = [[15, 83, 'variable', 'skills'],];
There is also no limit on how many arrays you can nest. The only thing is to be able to orient in them.
// arrays can sometime cause you a headache ... var extremeArr = [['sketch', 'design'],[56, true, [1, [[15, 165, 'star wars']], 'nested'], false],[12, 56, false]]; extremeArr; // will return 'star wars'
Let’s end it here so you can give yourself a break to grasp all the information and practice a bit with arrays. Make sure you understand this subject because you will deal with arrays very often in the future. I will also write some more posts on arrays to provide you with more material for learning. Thank you very much for your attention and see ya later. AD
If you liked this article, please subscribe so you don't miss any future post.
If you'd like to support me and this blog, you can become a patron, or you can buy me a coffee 🙂