Skip to content

Instantly share code, notes, and snippets.

@JeevanJain
Last active February 15, 2024 06:44
Show Gist options
  • Save JeevanJain/ba9d7927d096768877f3f63b39e46b6d to your computer and use it in GitHub Desktop.
Save JeevanJain/ba9d7927d096768877f3f63b39e46b6d to your computer and use it in GitHub Desktop.
Detailing Array
Operation Time Complexity
Access O(1)
Search O(n)
Insertion O(n)
Deletion O(n)

Array Literal

The simplest way is to use the array literal [] and list the elements inside.

let myArray = [1, 2, 3, 4, 5];

Array Constructor

You can use the Array constructor to create an array. You can either provide the array size or list elements as arguments.

let myArray = new Array(1, 2, 3, 4, 5);

Empty Array with a Defined Length

You can create an empty array with a specific length using the Array constructor.

let emptyArray = new Array(5);
// Results in an array with five undefined elements: [undefined, undefined, undefined, undefined, undefined]

Static Methods

Array.from()

  • Creates a new Array instance from an iterable or array-like object.
let newArray = Array.from('hello', char => char.toUpperCase());
// Results in ['H', 'E', 'L', 'L', 'O']

Array.isArray()

  • Returns true if the argument is an array, or false otherwise.
console.log(Array.isArray([1, 2, 3])); // true
console.log(Array.isArray("hello"));   // false

Array.of()

  • Creates a new Array instance with a variable number of arguments, regardless of number or type of the arguments.
let myArray = Array.of(1, 2, 3, 4, 5);

Array.fromAsync()

  • Creates a new Array instance from an async iterable, iterable, or array-like object.
async function fetchUserData(userId) {
  // Simulate an asynchronous call
  const response = await fetch(`https://api.example.com/users/${userId}`);
  return response.json();
}

(async () => {
  const userIds = [1, 2, 3];

  const users = await Array.fromAsync(userIds, async (userId) => {
    const user = await fetchUserData(userId);
    return user.name;
  });

  console.log(users); // ["John Doe", "Jane Doe", "Alice Smith"]
})();

Instance Property

  • These properties are defined on Array.prototype and shared by all Array instances

length

  • Reflects the number of elements in an array.
  let array = [1, 2, 3, 4, 5];
  console.log(array.length); // Outputs 5

Instance Methods

at

  • Returns the array item at the given index. Accepts negative integers, which count back from the last item.
const arr = [1, 2, 3, 4, 5];
let index = 2;
console.log(arr.at(index)); // Outputs 8

index = -2;
console.log(arr.at(index)); // Output 4

concat

  • Concatenates (joins) arrays or values to the calling array and returns a new array.
  let arr1 = [1, 2, 3];
  let arr2 = [4, 5, 6];
  let concatenatedArray = arr1.concat(arr2); // Outputs [1, 2, 3, 4, 5, 6]

join

  • Joins all elements of an array into a string, using the specified separator.
  let arr = ['apple', 'mango', 'orange'];
  console.log(arr.join(', '));  // Output: 'apple, mango, orange'
  console.log(arr.join('-'));  // Output: 'apple-mango-orange'

reverse

  • Reverses the order of the elements of an array in place. (First becomes the last, last becomes first.)
  • and returns the reference to the same array.
const array1 = ['one', 'two', 'three'];
array1.reverse();
console.log(array1); // ["three", "two", "one"]
[1, 2, 3, 4, 5].at(1); // 2
[1, 2, 3, 4, 5].pop(); // 5
[3, 4, 5, 61.push(7); // [3, 4, 5, 6, 7]
[3, 4, 5, 6].fill(1); // [1, 1, 1, 1]
Screenshot 2024-02-15 at 12 47 35 AM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment