Arrays In JavaScript

javascript

Class variables and data types in arrays (array) we have briefly mentioned the topic. Variables created to store more than one data are called arrays. The basic idea of creating arrays is to access multiple data under a single variable.

Each data in the variables is called an element. Array elements are called with index values. The index indicates where an element is located in the array.

Indexes start from 0 as in many programming languages. The reason for starting from 0 is that programming languages are completely human logic created.

There are three methods to define an element in the array. We saw the first one in the class I mentioned.

<script>
var sayilar = new Array("zero", "one", "two", "three");
document.write(sayilar);
</script>  
This is the method we’ve learned before. we created a series of four elements called numbers.
This is how our output will be.
<script>
var sayilar = ["zero", "one", "two", "three"];
document.write(sayilar);
</script>   

Javascript Replacing Array Elements

In JavaScript, array elements can be changed very easily.

<script>
var sayilar = ["zero", "one", "two", "three"];
sayilar[2] = "iikii";
document.write(sayilar);
</script>  
For example, we changed the “two” elements in the number series to “iikii”. To do this, we need to assign the new value after we type the index value of the element we want to change.

Javascript Add New Elements To The Array

Adding new elements to the array in JavaScript is also very easy.

push( ) method: push means pushing in our language. Used in Javascript to add an element to the end of the array.

<script>
var sayilar = ["zero", "one", "two", "three"];
sayilar.push("four", "five");
document.write(sayilar);
</script>     
That’s how we added two new guys.
unshift( ) method; this method is used to add elements to the beginning of the array in JavaScript.
<script>
var sayilar = ["sıfır", "bir", "iki", "üç"];
sayilar.unshift("eksi üç", "eksi iki", "eksi bir");
document.write(sayilar);
</script> 

Delete An Array Element

splice( ) method; splice means joining two ends together in our language. In JavaScript, it is used for elements or elements to be deleted after the specified index value. in the splice method, there are two parameters that are in replace. The first parameter specifies the index position to be deleted, and the second parameter specifies how many elements after the index will be deleted.

<script>
var sayilar = ["sıfır", "bir", "iki", "üç"];
sayilar.unshift("eksi üç", "eksi iki", "eksi bir");
sayilar.splice(0, 3);
document.write(sayilar);
</script>        
It may seem a little complicated. We did this here: we added negative values and then subtracted them from the array.

Javascript Delete and insert an element simultaneously from the array

Again, we use the splice( ) function. But this time, after two parameters, we write the next elements instead of the elements that we delete. if you fully understand splice (), this will be very easy.

<script>
var sayilar = ["sıfır", "bir", "iki", "üç"];
sayilar.unshift("eksi üç", "eksi iki", "eksi bir");
sayilar.splice(0, 3, "zinzin", "zomzom", "zumzum");
document.write(sayilar);
</script>    

Javascript Merging Arrays

concat( ) method; Used to combine multiple arrays.

<script>
var sayilar = ["sıfır", "bir", "iki", "üç"];
var cinler = ["zinzin", "zomzom", "zepzep"];
var sayilar_ve_cinler = sayilar.concat(cinler);
document.write(sayilar_ve_cinler);
</script>       

Javascript Sorting Of Elements

sort( ) method; Used to sort array elements by small to large.

<script>
var sayilar = ["sıfır", "bir", "iki", "üç"];
document.write(sayilar.sort());
</script>  

Leave A Reply

Your email address will not be published.