Skip to content

Instantly share code, notes, and snippets.

@JeevanJain
Last active February 14, 2024 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeevanJain/37a95d334a6ea9f2b4b083e8421ab3e8 to your computer and use it in GitHub Desktop.
Save JeevanJain/37a95d334a6ea9f2b4b083e8421ab3e8 to your computer and use it in GitHub Desktop.
JavaScript DataStructure

Arrays


  • An array is a data structure that stores a collection of elements, each identified by an index or a key.

  • Arrays provide a way to organize and access elements in a sequential manner.

  • Array indices in JavaScript start at 0. The first element is accessed using index 0, the second with index 1, and so on.

const arr = ['a', 'b', 'c', 'd'];
console.log(arr[1]); // b
  • Unlike some languages where arrays have a fixed size, JavaScript arrays are dynamic and can grow or shrink dynamically. You can add or remove elements at any time.
  • JavaScript arrays can store elements of different data types in the same array.
const mixedArray = [1, 'hello', true];
  • JavaScript supports multi-dimensional arrays as well. You can create arrays of arrays to represent matrices or tables.
let matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];
  • In JavaScript, arrays come with many built-in properties and methods we can use with different purposes, such as adding or deleting items from the array, sorting it, filtering its values, know its, length and so on.
  • MDN

What is Data Structure ?


  • A data structure is a way of organizing and storing data to perform operations efficiently. It defines a set of rules or algorithms for organizing and manipulating the data, making it easier to access, insert, delete, or modify information.

  • The data structures in javascript are classified into two categories - primitive (built in) and non-primitive (not built in) data structures.

  • A primitive data structure can store the value of only one data type.

    • integer, character, boolean, float, String.
    • Primitive data types are usually directly supported by the programming language and are not composed of other data types.
  • A non-primitive data structure can store by combining primitive data types or other non-primitive data types.

    • Arrays, linked lists, stacks, queues, trees.
    • They provide more flexibility and allow for the creation of complex data structures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment