Search This Blog

2024/03/25

Javascript : Arrays

One of the most important data structures in JavaScript is the array,
which is a collection of elements.

An array enables storing a collection of multiple items under a single
variable name & let us perform common operations on its elements.

How to declare an array in javascript ?

// Using array constructor
let array = new array("John Doe", 24, true);

Using the literal notation:
To declare an array with literal notation you just define a new array
using empty brackets. You will place all elements within the square
brackets and separate each item or element with a comma.

Code:
let array = ["John Doe", 24, true];

If array do not have elements at time of declartion then it can be
declared as follows
let myArray = [];

Declare an Array with the Array Constructor:
To declare an array with Array Constructor you we use new Array() syntax.

Code:
let myArray = new Array();
console.log(myArray);
Output:
[]

Here array declared has no element in it.

To declare array with element we need to pass the element as parameters
to new Array constructor,
each one seperated by comma.

Code:
let myArray = new Array("John Doe", 24, true);
console.log(myArray);

Output:
[ 'John Doe', 24, true ]

Note:
It is not compulsory to add new, as both Array() and new Array() perform
the same task.

Arrays are zero-indexed, meaning you can access each element starting from
zero or output the entire array.
Consider following code snippet.

Code:
let myArray = ["John Doe", 24, true];
console.log(myArray[0]); // 'John Doe'
console.log(myArray[2]); // true
console.log(myArray); // ['John Doe', 24, true]

Output:
John Doe
true
[ 'John Doe', 24, true ]

Same thing applies to array defined using new Array constructor.

Adding elements to Empty Array After Declaration:
You can first initialize an empty array, and then you assign values
to its elements at specific indices.

Code:
let myArray = []; // Array with three empty slots
myArray[0] ="John Doe"
myArray[1] =24
myArray[2] =true

console.log(myArray)

Output:
[ 'John Doe', 24, true ]

Samething happens even if we declare array using new Array constructor.
Below code will illustrate the same.
Code:
let myArray = new Array() // Array with three empty slots
myArray[0] ="John Doe"
myArray[1] =24
myArray[2] =true

console.log(myArray)

Output:
[ 'John Doe', 24, true ]

Length of an array:
A length of an array is basically number of items it holding.

Code:
let myArray = ["John Doe", 24, true];
console.log(myArray.length);

Output:
3
Explanation:
Here myArray contain 3 individual items hence length is 3.

Even if array element is empty slot it still get counted in length.

Code:
let myArray = [, , ,]; // Array with three empty slots
console.log(myArray.length); // Output: 3
console.log(myArray[0]); // Output: undefined
console.log(myArray[1]); // Output: undefined
console.log(myArray[2]); // Output: undefined
Output:
3
undefined
undefined
undefined

Explanation:
If you access one of these empty slots, it will return undefined
because there is no value assigned to that slot:

Array Constructor & only one numeric parameter:
Consider code below:

Code:
let myArray = new Array(4);
console.log(myArray);

Output:
[ <4 empty items> ]

Explanation:
In JavaScript, when you use the new Array() constructor with a
single argument (like new Array(4) in your example),
it creates an array with a specified length, filled with empty
slots. Empty slots are represented by commas in the array notation [,].
The array here is considered sparse because it has empty slots.

One might think if we passed a postive non integer real number as
only paraeter to new Array constructor what will happen
Lets consider following code snippet.

Code:
let myArray = new Array(4.78)
console.log(myArray)
Output:
RangeError: Invalid array length

length of an array cannot be non integer positive real number.

lets try to pass negative value as only parameter to new Array constructor.

Code:
let myArray = new Array(-4) // Array with three empty slots
console.log(myArray)

Output:
RangeError: Invalid array length

length of an array cannot be negative.

In javascript an array is an object,there are lot of methods defined over
array object to manipulate array elements.

No comments:

Post a Comment