0% found this document useful (0 votes)
2 views14 pages

4_Arrays

Arrays are used to store similar types of values and are objects that can be manipulated using various methods. Key operations include adding or removing elements with methods like push(), pop(), and splice(), which can modify the array or return new arrays. Unlike strings, which are immutable, arrays are mutable, allowing for direct value changes through indexing.

Uploaded by

Swadeep Sachan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views14 pages

4_Arrays

Arrays are used to store similar types of values and are objects that can be manipulated using various methods. Key operations include adding or removing elements with methods like push(), pop(), and splice(), which can modify the array or return new arrays. Unlike strings, which are immutable, arrays are mutable, allowing for direct value changes through indexing.

Uploaded by

Swadeep Sachan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Arrays

are used generally when we have to store similar type of values in a variable.

are objects in itself like typeOf() array is object.

let marks=[12,32,36,65,95];
[Link](marks);
[Link]("length of array is:",[Link]);

Since objects are key value pair and array is an object only but for our understanding we can think
like array is also a key:value pair where key is index and value as normal value only i.e. index:value.
If we go out of index then it gives undefined.

Values replace:
Strings – immutable and Arrays – mutable, hence we can’t change value of string thorugh index
while in case of arrays as we saw we can, see eg below.

String: it will be hello only.

Arrays:
logic

In this we can’t use “for of” loop as it directly gives the values but we have to store the resultant
values in array using index and then also if we want we can use in below way:
Array Methods:

Using few methods arrays get changed and for few methods we get new array like in the case of
strings.

[Link]() : adds element at the end:


[Link]() : It deletes the item from end and return.

Delete from end:

Delete with return:


[Link](): converts array to string with comma separated.

Above one doesn’t change the existing array rather it creates a new one.

[Link]():

It returns new array after concatenation, doesn’t change the existing arrays from which
concatenation has been done.

OR
5. shift() and unshift():

 shift() and unshift() methods relevance:


[Link]() : same like strings.
7. splice() :

start index – gets included


delCount – how many to be deleted
newElement – gets added from the start index(above one)
 When we want to add element at specific index using splice():

 When we want to delete element at spefic index using splice():


 When we want to replace element at spefic index using splice():
 Analyse below one:

You might also like