014_Working with Array and iterable

Introduction

An array in JavaScript is a type of global object that is used to store data. Arrays consist of an ordered collection or list containing zero or more datatypes, and use numbered indices starting from 0 to access specific items.

Arrays are very useful as they store multiple values in a single variable, which can condense and organize our code, making it more readable and maintainable. Arrays can contain any data type, including numbersstrings, and objects.

An iterable 

  • object that implement "iterable protocal" and have @@iterable method(i.e symbol iterator)
  •  object where you can use For-of-loop.
  • Not every iterable is array ! like Nodelist , string , map , set
Array like Object
  • Technically, object with length property and uses indexes to access them.

Creating Array :

// creating Array with array literal

const number = [1, 2, 3]; // most recommend way to create array

// creating array with constructor array

const moreNumber = new Array();

// this equals to empty array == []

const MoreNumber = new Array('hello', 'hi');

// output= 0:'hello' , 1:'hi' length : 2


// implications in an array

const numberException = new Array(5)

// output= length:5 : passing a single number will be intrepreted as a length of array that to be created
// an empty array of that length wil be created.


// Array.from() lets you create Arrays from:
//array-like objects (objects with a length property and indexed elements);


console.log(Array.from('foo'));
// expected output: Array ["f", "o", "o"]

// nested array: working with nested data

const dataAnalatyics = [
[1, 2, 3],
[4, 5, 6]
]
for (const data of dataAnalatyics) {
for (const dataPoint of data) {
console.log(dataPoint);
}
}

Comments

Popular posts from this blog

JavaScript — Double Equals vs. Triple Equals - weird things on JavaScript

06_Understanding Execution Context and Execution Stack in Javascript

Creating and inserting element- part-2