Arrays
- Arrays are variables that can hold more than one value.
- The values may also be of different types.
Example
const number = [1, 2, 3, 4];
const names = ["Jonh", "Tom", "Harry"];
const bool = [true, false];
const mixed = [1, "Ron", true, null, undefined];
console.log ("Number-array:", number);
console.log ("Names-array:", names);
console.log ("Bool-array:", bool);
console.log ("Mixed-array:", mixed);
Output
Getting access to Array Elements
- You can access array elements using index.
- Index start from 0.
Example
const names = ["Jonh", "Tom", "Harry"];
console.log ("At index 0:", names[0]);
console.log ("At index 1:", names[1]);
console.log ("At index 2:", names[2]);
Output
Change the value of array element.
- You need to access the array element using index and assign new value to it.
Example
const names = ["Jonh", "Tom", "Harry"];
names[0] = "Ron"
console.log ("At index 0:", names[0]);
Output
The length property
.length
property returns the length of array.- It always returns one more than the highest index of array.
Example
const names = ["Jonh", "Tom", "Harry"];
console.log ("Length of names array: ", names.length);
Output
Arrays Methods
- The built-in array attributes and methods are the true power of JavaScript arrays.
toString()
- It converts array to a string and all the value will be seperated with comma.
Example
const names = ["Jonh", "Tom", "Harry"];
console.log ("Length of names array: ", names.toString());
Output
join()
- It also acts like
toString()
but you can specify your seperator in this case.
Example
const names = ["Jonh", "Tom", "Harry"];
console.log ("Length of names array: ", names.join("-"));
Output
pop()
- Removes the last element from the array and returns it.
Example
const names = ["Jonh", "Tom", "Harry"];
console.log ("Before array: ", names);
console.log ("Using pop on array: ", names.pop());
console.log ("After array: ", names);
Output
push()
- Adds a new element at the end of the array and returns length of the array.
Example
const names = ["Jonh", "Tom", "Harry"];
console.log ("Before array: ", names);
console.log ("Using push on array: ", names.push("Ron"));
console.log ("After array: ", names);
Output
shift()
- Removes the first element and returns it.
Example
const names = ["Jonh", "Tom", "Harry"];
console.log ("Before array: ", names);
console.log ("Using shift on array: ", names.shift());
console.log ("After array: ", names);
Output
unshift()
- Adds element to the beginning and return length of the array.
Example
const names = ["Jonh", "Tom", "Harry"];
console.log ("Before array: ", names);
console.log ("Using unshift on array: ", names.unshift("Ron"));
console.log ("After array: ", names);
Output
concat()
- It joins array to the given array.
- Note - It returns new array, It does not change the original array
Example
const names = ["Jonh", "Tom", "Harry"];
const number = [1, 2, 3, 4];
console.log ("Before array: ", names);
console.log ("Using concat on array: ", names.concat(number));
Output
sort()
- It sorts array alphabetically.
- Note - It modifies the original array
Example
const names = ["Jonh", "Tom", "Harry"];
console.log ("Before array: ", names);
console.log ("Using sort on array: ", names.sort());
Output
splice()
- It can be use to add elements to a specific place and can remove some elements.
Example
const names = ["Jonh", "Tom", "Harry"];
console.log ("Before array: ", names);
console.log ("Using splice on array: ", names.splice(1, 2, "Jerry", "Ron"));
console.log ("Before array: ", names);
Output
Description
slice()
- It slices out a peice of an array.
- Note - It creates a new array and returns it
Example
const number = [1, 2, 3, 4, 5];
console.log ("Before array: ", number);
console.log ("Using slice on array: ", number.slice(4));
console.log ("Using slice on array: ", number.slice(1, 3));
Output
Description
And that's all from my side
I hope this reference guide has been helpful for you.