Array In JavaScript
An Array in JavaScript is define as a data structure used to stored multiple value of different datatypes, means array in javascript can hold different datatypes in single variable.
Indexing of Array Element starts from 0 (ZERO).
2 Different ways to create of Array In JavaScript:
- Creating Literal array:
// Creating an Empty Array
let arr = [];
console.log(arr);
// Creating an Array and Initializing with Values
let arr= [10, "sahiba", 32];
console.log(arr);
- Create using new Keyword (Constructor):
// Creating and Initializing an array with values
let arr = new Array(1, "sahiba", true);
console.log(arr);
Operations to perform on Array:
- Length of an array:
let arr = [1, "sahiba", true];
console.log(arr.length);
- Get First Value of an array:
let arr = [1, "sahiba", true];
console.log(arr[0]);
- Get Last Value of an array:
let arr = [1, "sahiba", true];
console.log(arr[arr.length-1]);
- Print all values of an array:
Using loop:
let arr = [1, "sahiba", true];
for(let i=0; i<arr.length; i++)
{
console.log(arr[i]);
}
Using ForEach Method:
let arr=[10,20,"sahiba"];
arr.forEach((item) => {console.log(item);});
let arr=[10,20,"sahiba"];
arr.forEach((item) => {
console.log(item);
});
- Modify Value of an array:
let arr = [1, "sahiba", "khurana"];
arr[0] = "Ms";console.log(arr);
let arr = [1, "sahiba", "khurana"];
arr[0] = "Ms";
console.log(arr);
- Increase and Decrease the Array Length:
let arr = [1, "sahiba", "khurana"];
//Increase the array length
arr.length=5;console.log("After increasing length=", arr.length);
let arr = [1, "sahiba", "khurana"];
//Increase the array length
arr.length=5;
console.log("After increasing length=", arr.length);
//Decrease the array length
arr.length=2;console.log("After decreasing length=", arr.length);
//Decrease the array length
arr.length=2;
console.log("After decreasing length=", arr.length);
- Conversion of an Array to String:
let arr = [1, "sahiba", "khurana"];
//Convert array to String
console.log(arr.toString());
let arr = [1, "sahiba", "khurana"];
//Convert array to String
console.log(arr.toString());
forEach() Method in Array:
The
forEach()
method in JavaScript is used to iterate over elements in an array. It executes a provided function once for each array element in order. It is a simpler and more readable way to perform operations on each element of an array compared to using traditional for
or while
loops.Syntax:
array.forEach(callback(currentValue, index, array), thisArg);
Parameters:
callback: A function to execute on each element in the array. It takes three arguments:
- currentValue: The current element being processed.
- index (optional): The index of the current element.
- array (optional): The array
forEach()
was called upon.
thisArg (optional): A value to use as
this
when executing the callback function.
Key Characteristics:
- Non-returning:
forEach()
doesn’t return a new array; it always returnsundefined
. Use it when you don't need a new array as the result of the operation. - Non-breaking: You cannot use
break
orcontinue
to terminate the loop. If you need to break the iteration, consider using afor
loop or afor...of
loop instead. - Mutability: It can be used to mutate the original array if necessary but doesn't create a new array.
it's very help full to understand the array in javascript
ReplyDeleteThank you :-)
Delete