How to use JavaScript Array includes()

Program to use JavaScript Array includes() method


let a = ['Cat', 'Dog', 'Tiger', 'Lion', 'Elephant'];
let b = a.includes('Lion');
console.log(b);

Output


true

Steps Description

  1. Declare two variable a and b
  2. Initialize two variable a and b with value of array a = ['Cat', 'Dog', 'Tiger', 'Lion', 'Elephant'] and b = a.includes('Lion')
  3. Array includes() method checks the statement in all elements of the given array, returns true if found, false if not found
  4. The value of b variable is displayed with the help of console.log inbuilt function

Program to use JavaScript Array includes() method


let c = [1,2,3,4,5];
let d = c.includes(8);
console.log(d);

Output


false

Steps Description

  1. Declare two variable c and d
  2. Initialize two variable c and d with value of array c = [1,2,3,4,5] and d = c.includes(8)
  3. Array includes() method checks the statement in all elements of the given array, returns true if found, false if not found
  4. The value of d variable is displayed with the help of console.log inbuilt function

How to use JavaScript Array.from()

Program to use JavaScript Array.from() method


let a = 'JavaScript';
let b = Array.from(a);
console.log(b);

Output


[
    'J', 'a', 'v', 'a',
    'S', 'c', 'r', 'i',
    'p', 't'
]

Steps Description

  1. Declare two variable a and b
  2. Initialize two variable a and b with value of string a = 'JavaScript' and b = Array.from(a)
  3. Array.from() method creates new array from array element
  4. The value of b variable is displayed with the help of console.log inbuilt function

How to use JavaScript Array forEach()

Program to use JavaScript Array forEach() method


let a = [3, 5, 7, 4];

let b = 0;
a.forEach((c) => {
  b += c;
});

console.log(b);

Output


19

Steps Description

  1. Declare two variables a and b
  2. Initialize two variable with value of string a = [3, 5, 7, 4] and b = 0
  3. The array foreach() method calls a function for all elements of an array.
  4. The value of b variable is displayed with the help of console.log inbuilt function

How to use JavaScript Array flatMap()

Program to use JavaScript Array flatMap() method


let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let b = a.flatMap((a) => a * 3);
console.log(b);

Output


[
    3,  6,  9, 12, 15,
   18, 21, 24, 27, 30
]

Steps Description

  1. Declare two variables a and b
  2. Initialize two variables a and b with value of string a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and b = a.flatMap((a) => a * 3)
  3. Array flatMap() method map all array elements and creates a new array.
  4. The value of b variable is displayed with the help of console.log inbuilt function

How to use JavaScript Array flat()

Program to use JavaScript Array flat() method


let a = [[1,2,3],[4,5],[6,7]];
let b = a.flat();
console.log(b);

Output


[
    1, 2, 3, 4,
    5, 6, 7
]

Steps Description

  1. Declare two variables a and b
  2. Initialize two variables a and b with value of strings a = [[1,2,3],[4,5],[6,7]] and b
  3. The array flat() method converts a nested array to a new array.
  4. The value of b variable is displayed with the help of console.log inbuilt function

Program to use JavaScript Array flat() method


let c = [[1,2],[3,4,5,[6,7],8],[9,10]];
let d = c.flat(2);
console.log(d);

Output


[
    1, 2, 3, 4,  5,
    6, 7, 8, 9, 10
]

Steps Description

  1. Declare two variables c and d
  2. Initialize two variables c and d with value of strings c = [[1,2],[3,4,5,[6,7],8],[9,10]] and d = c.flat(2)
  3. The array flat() method converts a nested array to a new array.
  4. The value of d variable is displayed with the help of console.log inbuilt function

How to use JavaScript Array findIndex()

Program to use JavaScript Array findIndex() method


let a = [15, 9, 2, 13, 24, 21];

let b = a.findIndex(function (c) {
    return c > 18;
});

console.log(b);

Output


4

Steps Description

  1. Declare three variables a, b and c
  2. Initialize three variables a, b and c with value of string a = [15, 9, 2, 13, 24, 21], b = a.findIndex(function (c) and c > 18
  3. Array findIndex() method finds the given statement in array element if any statement element matches then prints its index number
  4. The value of b variable is displayed with the help of console.log inbuilt function

How to use JavaScript Array find()

Program to use JavaScript Array find() method


let a = [5, 9, 3, 13, 18, 21];

let b = a.find(function (c) {
    return c > 9;
});

console.log(b);

Output


13

Steps Description

  1. Declare three variables a, b and c
  2. Initialize three variables a, b and c with value of string a = [5, 9, 3, 13, 18, 21], b = a.find(function (c) and c > 9
  3. Array find() method find array element if any statement element matches then return its value
  4. The value of b variable is displayed with the help of console.log inbuilt function

How to use JavaScript Array every()

Program to use JavaScript Array every() method


function evennumber(number) {
    return number % 2 == 0;
}
let a = [3, 5, 7, 23, 13];
let b = a.every(evennumber);
console.log(b);

Output


false

Steps Description

  1. Declare function evennumber
  2. Declare two variables a and b
  3. Initialize two variables a and b with value of string a = [3, 5, 7, 23, 13] and b = a.every(evennumber)
  4. Array every() method checks array every element and returns boolean value
  5. The value of b variable is displayed with the help of console.log inbuilt function

How to use JavaScript Array entries()

Program to use JavaScript Array entries() method


let a = [ 'Cat', 'Dog', 'Tiger', 'Lion', 'Elephant' ];
let b = a.entries();
for(let c of b){
   console.log(c);
}

Output


[ 0, 'Cat' ]
[ 1, 'Dog' ]
[ 2, 'Tiger' ]
[ 3, 'Lion' ]
[ 4, 'Elephant' ]

Steps Description

  1. Declare three variables a, b and c
  2. Initialize three variables with array value respectively a = [ 'Cat', 'Dog', 'Tiger', 'Lion', 'Elephant' ], b = a.entries() and c
  3. Array entries() method returns the index number of the array.
  4. The value of c variable is displayed with the help of console.log inbuilt function

Program to use JavaScript Array entries() method


let d = [3,5,7,2,8];
let e = d.entries();
for(let f of e){
   console.log(f);
}

Output


[ 0, 3 ]
[ 1, 5 ]
[ 2, 7 ]
[ 3, 2 ]
[ 4, 8 ]

Steps Description

  1. Declare three variables d, e and f
  2. Initialize three variables with array value respectively d = [3,5,7,2,8], e = d.entries() and f
  3. Array entries() method returns the index number of the array.
  4. The value of f variable is displayed with the help of console.log inbuilt function

How to use JavaScript Array concat()

Program to use JavaScript Array concat() method


let a = ['Cat', 'Dog'];
let b = ['Tiger', 'Lion', 'Elephant'];
let c = a.concat(b);
console.log(c);

Output


[ 'Cat', 'Dog', 'Tiger', 'Lion', 'Elephant' ]

Steps Description

  1. Declare three variables a, b and c
  2. Initialize three variables with array value respectively a = ['Cat', 'Dog'], b = ['Tiger', 'Lion', 'Elephant'] and c = a.concat(b)
  3. In Concat() array method, an array can be concatenated to more than one array.
  4. The value of c variable is displayed with the help of console.log inbuilt function

Program to use JavaScript Array concat() method


let d = ['Peas', 'Carrot', 'Potato'];
let e = ['Tomato', 'Brinjal', 'Cabbage'];
let f = ['Green Chilli', 'Green Coriander']
let g = d.concat(e,f);
console.log(g);

Output


[
    'Peas',
    'Carrot',
    'Potato',
    'Tomato',
    'Brinjal',
    'Cabbage',
    'Green Chilli',
    'Green Coriander'
]

Steps Description

  1. Declare four variables d, e, f and g
  2. Initialize four variables with array value respectively d = ['Peas', 'Carrot', 'Potato'], e = ['Tomato', 'Brinjal', 'Cabbage'], f = ['Green Chilli', 'Green Coriander'] and g = d.concat(e,f)
  3. In Concat() array method, an array can be concatenated to more than one array.
  4. The value of g variable is displayed with the help of console.log inbuilt function

Program to use JavaScript Array concat() method


let h = [1,2,3];
let i = [4,5];
let j = [6,7,8];
let k = [9,10];
let l = h.concat(i,j,k);
console.log(l);

Output


[
    1, 2, 3, 4,  5,
    6, 7, 8, 9, 10
]

Steps Description

  1. Declare five variables h, i, j, k and l
  2. Initialize five variables with array value respectively h = [1,2,3], i = [4,5], j = [6,7,8], k = [9,10] and l = h.concat(i,j,k)
  3. In Concat() array method, an array can be concatenated to more than one array.
  4. The value of l variable is displayed with the help of console.log inbuilt function