Array In Javascript

Array In Javascript

ยท

7 min read

What is an array?

Bascically we write array in square brackets [], and there should be the value of number, string and boolean or empty. Exp Arr=[]; . In other word we can say array is used to collection of the values.

The Array object, as with arrays in other programming languages, enables storing a collection of multiple items under a single variable name, and has members for performing common array operations.

Descriptions:

In JavaScript, arrays aren't primitives but are instead Array objects with the following core characteristics:

  • JavaScript arrays are resizable and can contain a mix of different data types. (When those characteristics are undesirable, use typed arrays instead.)

  • JavaScript arrays are not associative arrays and so, array elements cannot be accessed using arbitrary strings as indexes, but must be accessed using nonnegative integers (or their respective string form) as indexes.

  • JavaScript arrays are zero-indexed: the first element of an array is at index 0, the second is at index 1, and so on โ€” and the last element is at the value of the array's length property minus 1.

  • JavaScript array-copy operations create shallow copies. (All standard built-in copy operations with any JavaScript objects create shallow copies, rather than deep copies).

  • Array Index

  • Index in array is always tells us the position of the value in array.

  • Exp:

  • \>Arr=['Azhar', 'Ansari', 2, 44, 45];

  • \> index = [0, 1, 2, 3, 4];

    C# Arrays (With Easy Examples)

    Array in Depth

  • A method is a function which is a property of an object:

    Some JavaScript array methods and how to use them - DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

Push()

Push() method is used to insert a new value to the array.

The push() method adds one or more elements to the end of an array and returns the new length of the array.

Example:

let names = new Array('name1', 'name2');  //a way to declare the array// not record
//Push => insert new values into the array
names.push('Cash');
console.log(names);   

Output    
[ 'name1', 'name2', 'Cash' ]

Slice()

The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.

Example:

let names =['Azhar', 'Deepak', 'Imran', 'Sumant', 'Tipu'];
console.log(names.slice(1, 3)); //3 is the excluded value not included value

Output
[ 'Deepak', 'Imran' ]

Splice()

The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. To access part of an array without modifying it, see slice().

Example:

let fruit = ['Apple', 'badaapple', 'chota Apple', 'double apple'];
fruit.splice(1, 3 ,'accha apple', 'kharab Apple');
console.log(fruit);

Output
[ 'Apple', 'accha apple', 'kharab Apple' ]

Concatenation()

The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

Example:

let arr1 = [1,2,3,4,5];
let arr2 = [4,5,6,7,8];
let arr3 = [7,7,7,7,7,7,7,7,7,]

console.log(arr1.concat(arr2));
console.log(arr1.concat(arr2, arr3));

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

Fill()

The fill() method changes all elements in an array to a static value, from a start index (default 0) to an end index (default array.length). It returns the modified array.

Example:

let arr4 = [1,2,3,4,5,6,7,8,9];
arr4.fill('Azhar', 1, 6);
console.log(arr4);

Output
[
  1,       'Azhar',
  'Azhar', 'Azhar',
  'Azhar', 'Azhar',
  7,       8,      
  9
]

Includes()

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

Example:

let num =[1,2,3,4,5,6,7,8];
console.log(num.includes(7,));

Output
true

Indexof()

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

Example:

let num =[1,2,3,'Azhar', 5,8,9,7,'Azhar', 'Azhar'];
console.log(num.indexOf('Azhar'));

Output
3

Isarray()

The Array.isArray() static method determines whether the passed value is an Array.

Description:

Array.isArray() checks if the passed value is an Array. It does not check the value's prototype chain, nor does it rely on the Array constructor it is attached to. It returns true for any value that was created using the array literal syntax or the Array constructor. This makes it safe to use with cross-realm objects, where the identity of the Array constructor is different and would therefore cause instanceof Array to fail.

Example:

let num =[1,2,3,'Azhar', 4,5,8, 'Azhar'];
let num1 ='Azhar';
console.log(Array.isArray(num));

Output
true
console.log(Array.isArray(num1));

Output
false

Join()

The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

Example:

let arr1 =[1,2,3,4,5,6];
let var1 =arr1.join('');

console.log(var1);
Output
123456

console.log(arr1.join(''));
Output
123456

console.log(typeof arr1);
Output
object

Keys()

The keys() method returns a new Array Iterator object that contains the keys for each index in the array.

Example:

const array1 = ['a', 'b', 'c'];
const iterator = array1.keys();

for (const key of iterator) {
  console.log(key);
}

Output
// expected output: 0
// expected output: 1
// expected output: 2

lastindexof()

The lastIndexOf() method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.

Example:

let num =[1,2,'Azhar',3,4,5,6,'Azhar'];

console.log(num.lastIndexOf('Azhar'));
Output
7

console.log(num.indexOf('Azhar'));
Output
2

console.log(num.indexOf(5));
Output
5

map()

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

Example:

let maths =[1, 4, 9, 16, 25,];
console.log(maths.map(Math.sqrt));

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

pop()

The pop() method removes the last element from an array and returns that element. This method changes the length of the array.

Example:

let maths =[1,4,9,16,25];

console.log(maths.pop());
Output
25

console.log(maths);
Output
[ 1, 4, 9, 16 ]

reverse()

The reverse() method reverses an array in place and returns the reference to the same array, the first array element now becoming the last, and the last array element becoming the first. In other words, elements order in the array will be turned towards the direction opposite to that previously stated.

Example:

let maths =[1,4,9,16,25];
console.log(maths.reverse());

Output
[ 25, 16, 9, 4, 1 ]

shift()

The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.

Example:

let maths = [88,'Azhar','Ansari',1,4,9,16,25];

console.log(maths.shift());
Output
88

console.log(maths);
Output
[ 'Azhar', 'Ansari', 1, 4, 9, 16, 25 ]

sort()

The sort() method sorts the elements of an array in place and returns the reference to the same array, now sorted. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

let names =['Azhar','Sumant', 'Imran', 'Deepak','Tipu'];

console.log(names.sort());
Output
[ 'Azhar', 'Deepak', 'Imran', 'Sumant', 'Tipu' ]

console.log(names.reverse(names.sort()));
Output
[ 'Tipu', 'Sumant', 'Imran', 'Deepak', 'Azhar' ]

unshift()

The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.

Example:

let fruit =['Apple','badaapple','chhota apple', 'doubleapple'];
fruit.unshift('apple01','apple02');
console.log(fruit);

Output
[
  'apple01',     
  'apple02',     
  'Apple',       
  'badaapple',   
  'chhota apple',
  'doubleapple'  
]

converting to array

It's convert elements to array by seperating the coma (,)

Example:

let name ='Azhar';
let array1= name.split("");
console.log(array1);

Output
[ 'A', 'z', 'h', 'a', 'r' ]

For of

Example:

let fruit =['Apple','badaapple','chhota apple', 'doubleapple'];
let upperfruit =[]; //empty array

for (let Azhar of fruit){
    upperfruit.push(Azhar.toUpperCase());
}
console.log(upperfruit);

Output
[ 'APPLE', 'BADAAPPLE', 'CHHOTA APPLE', 'DOUBLEAPPLE' ]

break() and continue()

Example:

for (let i = 0; i<=5; i++){
    if (i==3){
        break;  //skip omit
    }
    console.log(i);
}

Output
0
1
2

///////////////////////////////////////////////////////////////////////
for (let i = 0; i<=5; i++){
    if (i==3){
        continue;  //skip omit
    }
    console.log(i);
}

Output
0
1
2
4
5
ย