-
-
Save defields923/18ddf7a3e820487a701ddce41299b8f2 to your computer and use it in GitHub Desktop.
ArraysArray studies// source https://jsbin.com/dikixab
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
// -----Arrays----- // | |
/* Arrays are list-like objects with a large number of mutational and accessible | |
methods. They can be assigned to variables and are enclosed with squar brackets: */ | |
"use strict"; | |
var exArray = ["I'm element 0", "I'm element 1", "Care to guess?"]; | |
/* As the example demonstrates, arrays are zero-based indexed, and elements can | |
be accessed by referring to its index: */ | |
console.log(exArray[1]); // logs "I'm element 1" | |
/* Dot-notation syntax will return an error: */ | |
// console.log(exArray.1); // ERROR, or just won't log anything | |
/* While technically Javascript considers arrays as objects, it is common to need | |
to check if an object is an array; the following method will check and return | |
a boolean value: */ | |
console.log(Array.isArray(exArray)); // true | |
var notArray = {}; | |
console.log(Array.isArray(notArray)); // false | |
/* The .length method will return the numeric length of the array's elements: */ | |
console.log(exArray.length); // logs 3 | |
/* Not to be confused with each element's zero-based index, .length begins counting | |
at 1. The previous example counts three elements and logs "3." This method can | |
also be used to log the very last element on an array no matter how long it is.*/ | |
console.log(exArray[exArray.length - 1]); // logs "Care to guess?" | |
/* Altering an element in an array is as simple as using its index number: */ | |
exArray[0] = "I was the third element all along!" | |
console.log(exArray); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment