How to use JavaScript Array splice()

Program to use JavaScript Array splice() method


let a = [1, 2, 3, 7, 5];
let b = a.splice(3, 1, 4);

console.log(a);
console.log(b);

Output


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

Steps Description

  1. Declare two variable a and b
  2. Initialize two variable with value of array a = [1, 2, 3, 7, 5] and b = a.splice(3, 1, 4)
  3. Array splice() method adds or removes an array element and returns the removed element.
  4. The value of b variable is displayed with the help of console.log inbuilt function

Program to use JavaScript Array splice() method


let c = [2, 44, 32, 8, 10, 12, 14, 16, 18, 20];
let d = c.splice(1, 2, 4, 6)

console.log(c);
console.log(d);

Output


[
    2,  4,  6,  8, 10,
   12, 14, 16, 18, 20
 ]
 [ 44, 32 ]

Steps Description

  1. Declare two variable c and d
  2. Initialize two variable with value of array c = [2, 44, 32, 8, 10, 12, 14, 16, 18, 20] and d = c.splice(1, 2, 4, 6)
  3. Array splice() method adds or removes an array element and returns the removed element.
  4. The value of d variable is displayed with the help of console.log inbuilt function

Program to use JavaScript Array splice() method


let e = ['Cat', 'Dog', 'Tiger', 'Lion'];
let f = e.splice(2, 0, 'Elephant', 'Bear');

console.log(e);
console.log(f);

Output


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

Steps Description

  1. Declare two variable e and f
  2. Initialize two variable with value of array e = ['Cat', 'Dog', 'Tiger', 'Lion'] and f = e.splice(2, 0, 'Elephant', 'Bear')
  3. Array splice() method adds or removes an array element and returns the removed element.
  4. The value of f variable is displayed with the help of console.log inbuilt function

How to use JavaScript Array sort()

Program to use JavaScript Array sort() method


let a = [1,3,5,2,4];
let b = a.sort();

console.log(b);

Output


[ 1, 2, 3, 4, 5 ]

Steps Description

  1. Declare two variable a and b
  2. Initialize two variable with value of array a = [1,3,5,2,4] and b = a.sort()
  3. Array sort() method sorts the array from descending to ascending order.
  4. The value of b variable is displayed with the help of console.log inbuilt function

Program to use JavaScript Array sort() method


let c = [ 'Cat', 'Dog', 'Tiger', 'Lion', 'Elephant' ];
let d = c.sort();

console.log(d);

Output


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

Steps Description

  1. Declare two variable c and d
  2. Initialize two variable with value of array c = [ 'Cat', 'Dog', 'Tiger', 'Lion', 'Elephant' ] and d = c.sort()
  3. Array sort() method sorts the array from descending to ascending order.
  4. The value of d variable is displayed with the help of console.log inbuilt function

How to use JavaScript Array some()

Program to use JavaScript Array some() method


function a(b) {
    return b < 18;
}

let c = [44, 23, 37, 60, 75];
let d = c.some(a);

console.log(d);

Output


false

Steps Description

  1. Declare three variable a, c and d
  2. Initialize three variable with value of array function a(b) { return b < 18;}, c = [44, 23, 37, 60, 75] and d = c.some(a)
  3. Array some() method returns true if the statement given in the array is related to a single element of the array, otherwise it returns false.
  4. The value of b variable is displayed with the help of console.log inbuilt function

How to use JavaScript String slice()

Program to use JavaScript String slice()


let a = 'Hello World JavaScript';
let b = a.slice(0, 11);

console.log(b);

Output


Hello World

Steps Description

  1. Declare two variable a and b
  2. Initialize two variable a and b with value of string a = 'Hello World JavaScript' and b = a.slice(0, 11)
  3. String slice() method String can be sliced by giving statement
  4. The value of b variable is displayed with the help of console.log inbuilt function

Program to use JavaScript String slice()


let c = 'Hello World JavaScript';
let d = c.slice(11, 22);

console.log(c.length);
console.log(d);

Output


22
JavaScript

Steps Description

  1. Declare two variable c and d
  2. Initialize two variable c and d with value of string c = 'Hello World JavaScript' and d = c.slice(11, 22)
  3. String slice() method String can be sliced by giving statement
  4. The value of d variable is displayed with the help of console.log inbuilt function

How to use JavaScript Array shift()

Program to use JavaScript Array shift() method


let a = [1,2,3,4,5];
let b = a.shift();

console.log(a);
console.log(b);

Output


[ 2, 3, 4, 5 ]
1

Steps Description

  1. Declare two variable a and b
  2. Initialize two variable with value of array a = [1,2,3,4,5] and b = a.shift()
  3. The array shift() method removes the first element of an array.
  4. The value of b variable is displayed with the help of console.log inbuilt function

Program to use JavaScript Array shift() method


let c = ['Good Morning', 'Good Afternoon', 'Good Evening', 'Good Night'];
let d = c.shift();

console.log(c);
console.log(d);

Output


[ 'Good Afternoon', 'Good Evening', 'Good Night' ]
Good Morning

Steps Description

  1. Declare two variable c and d
  2. Initialize two variable with value of array c = ['Good Morning', 'Good Afternoon', 'Good Evening', 'Good Night'] and d = c.shift()
  3. The array shift() method removes the first element of an array.
  4. The value of d variable is displayed with the help of console.log inbuilt function

How to use JavaScript Array reverse()

Program to use JavaScript Array reverse() method


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

Output


[ 5, 4, 3, 2, 1 ]

Steps Description

  1. Declare two variable a and b
  2. Initialize two variable with value of array a = [1,2,3,4,5] and b = a.reverse()
  3. The array reverse() method returns the given array in reverse order of the array elements.
  4. The value of b variable is displayed with the help of console.log inbuilt function

Program to use JavaScript Array reverse() method


let c = ['Cat', 'Dog', 'Tiger', 'Lion', 'Elephant'];
let d = c.reverse();
console.log(d);

Output


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

Steps Description

  1. Declare two variable c and d
  2. Initialize two variable with value of array c = ['Cat', 'Dog', 'Tiger', 'Lion', 'Elephant'] and d = c.reverse()
  3. The array reverse() method returns the given array in reverse order of the array elements.
  4. The value of d variable is displayed with the help of console.log inbuilt function

How to use JavaScript Array pop()

Program to use JavaScript Array pop() method


let a = ['Good Morning', 'Good Afternoon', 'Good Evening', 'Good Night'];
let b = a.pop();
console.log(b);

Output


Good Night

Steps Description

  1. Declare two variable a and b
  2. Initialize two variable with value of array a = ['Good Morning', 'Good Afternoon', 'Good Evening', 'Good Night'] and b = a.pop()
  3. Array pop() method removes the last element of the array. and returns the removed element
  4. The value of b variable is displayed with the help of console.log inbuilt function

Program to use JavaScript Array pop() method


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

Output


5

Steps Description

  1. Declare two variable c and d
  2. Initialize two variable with value of array c = [1,2,3,4,5] and d = c.pop()
  3. Array pop() method removes the last element of the array. and returns the removed element
  4. The value of d variable is displayed with the help of console.log inbuilt function

How to use JavaScript Array map()

Program to use JavaScript Array map() method


let a = [1, 3, 5, 8];
let b = a.map(c => c ** 2);

console.log(b);

Output


[ 1, 9, 25, 64 ]

Steps Description

  1. Declare two variables a and b
  2. Initialize two variables a and b with value of array a = [1, 3, 5, 8] and b = a.map(c => c ** 2)
  3. array map() method map all array elements with statement 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 length

Program to use JavaScript Array length


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

Output


5

Steps Description

  1. Declare two variable a and b
  2. Initialize two variable with value of array a = ['Cat', 'Dog', 'Tiger', 'Lion', 'Elephant'] and b
  3. The array length() method outputs the index numbers of the array elements.
  4. The value of b variable is displayed with the help of console.log inbuilt function

Program to use JavaScript Array length


let c = ['Cat', 'Dog', 'Tiger', 'Lion', 'Elephant'];
c.length = 3
console.log(c);

Output


[ 'Cat', 'Dog', 'Tiger' ]

Steps Description

  1. Declare variable c
  2. Initialize variable with value of array c = ['Cat', 'Dog', 'Tiger', 'Lion', 'Elephant']
  3. The array length() method outputs the index numbers of the array elements.
  4. The value of c variable is displayed with the help of console.log inbuilt function

Program to use JavaScript Array length


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

Output


10

Steps Description

  1. Declare two variable d and e
  2. Initialize two variable with value of array d = [1,2,3,4,5,6,7,8,9,10] and e = d.length
  3. The array length() method outputs the index numbers of the array elements.
  4. The value of e variable is displayed with the help of console.log inbuilt function