DEV Community

Cover image for If You Don’t Know These Array Methods, We Need to Talk.
Kunal
Kunal

Posted on

If You Don’t Know These Array Methods, We Need to Talk.

Hi everyone ! In this blog , we will discuss about Array and its method in JavaScript that commonly used nowadays that will help you to improve your production code and expand your knowledge .

Topics to Cover :

  1. Datatypes
  2. What is Array and why to use it
  3. Array methods :
    • push() and pop()
    • shift() and unshift()
    • map()
    • filter()
    • reduce() (basic explanation only)
    • forEach()

Datatypes

In JavaScript , Datatypes defines what kind of value you can hold inside a variable.
Think of data types like different types of containers in real life :

  • A water bottle holds water
  • A wallet holds money
  • A folder holds document

There are 8 different kind of datatypes in JavaScript.

1. String :- Text inside quotes

eg:- let name = "Like this blog"
Enter fullscreen mode Exit fullscreen mode

2. Boolean :- Only two values

eg:- True , False 
Enter fullscreen mode Exit fullscreen mode

3. BigInt :- For very very large numbers.

eg:- let bigNumber = 12345678901234567890n;
Enter fullscreen mode Exit fullscreen mode

4. Number :- Any normal number.

eg:- let marks = 95;
     let price = 99.99;

Enter fullscreen mode Exit fullscreen mode

5. Undefined :- When a variable and no value is given

eg:- let x;
Enter fullscreen mode Exit fullscreen mode

6. Symbol :- Creates a unique value.

eg:- let id = Symbol("id");
Enter fullscreen mode Exit fullscreen mode

7. Null :- Means intentionally empty.

eg:- let data = null;
Enter fullscreen mode Exit fullscreen mode

8. Object :- Stores multiple values together

eg:-  let user = {
  name: "Kunal",
  age: 21
};

Enter fullscreen mode Exit fullscreen mode

Note :- In JavaScript datatype of Array is object.


Array and why to use array

An Array is a data structure that contains list of element which store multiple value under a single variable.

To declare an Array in JavaScript you can use let , var , const keywords with square bracket and enclose all the element with them.

let items = ["like" , "This" , "blog"]
Enter fullscreen mode Exit fullscreen mode

In JavaScript you can assign any datatypes inside an array.

ds

But why we use Array ?

We use array for the following :

  • To store multiple values inside one variable
  • To access item using index ( position )
  • To loop through data easily

Array Methods Every Developer Should Know

Array are the fundamental part of programming language. They allow you to store , manipulate and access multiple values in a single variable

Array do have some inbuilt methods that helps us to manipulate the Array easily

Lets see one by one

1. push() and pop()

  • push() :- The push() methods adds one or more elements to the end of an array

push

  • pop() :- The pop() method removes the last element of an array

pop

2. shift() and unshift()

  • shift() :- Similar to push and pop , shift() removes the first element of an array

shift

  • unshift() :- unshift() add one or more element to the beginning of array

unshift

3. map()

The map() method creates a new array by applying a function to each element of the original array.

map

4. filter()

The filter() method creates a new array with all elements that pass the condition provided by the function.

filter

Note :- It will only return the element with fulfill the condition of the function rest will be ignored.

5. reduce()

The reduce() method executes a reducer function on each element of the array, reducing in single output value.

reduce()

6. forEach()

The forEach() method executes a provided function once for each array element.

forEach()

These methods provide powerful tools for managing and manipulating data structures more effectively. As you integrate these methods into your daily coding practices, you’ll find they can dramatically improve the quality and readability of your code, enabling you to build more efficient products.


Thanks for reading ! if enjoyed this blog , you can read more on this 👇

Top comments (0)