Javascript Array Creation
Let's start by answering the question "what is a TV series?" An array is a special variable in which similar expressions are brought together. While one data can be stored in a variable, it is possible to store more than one data in arrays. In the first line of the example code snippet below, an array named languages is created. In the second line, the label whose id is the sample is selected and the values in the array are placed.
var languages= ["CSS", "HTML", "JavaScript"];
document.getElementById("example").innerHTML = languages;
If we did not use an array, we would have to write the above values as follows.
var language1="CSS";
var language2="HTML";
var language3="JavaScript";
How to Create an Array with Javascript?
The rule for creating JS arrays is as follows.
var arrayname=["value1","value2","value3",...];
The example code will be as follows.
var hardware=["Harddisk","Processor","Ram","Motherboard"];
Another thing that should be known is that data types are defined dynamically with JavaScript. Based on this statement, the types of data used in the javascript array may differ from each other. It is possible to define an array as follows.
var example=["Ahmet",false,12,"Text",78.5];
Note: Not only in this way, it is also possible to define nested arrays and objects within the array (arrays are also objects, by the way).
New Creating an Array with Array
To create an array with the new keyword, you will need to write code as follows.
var names=new Array("Ali","Ahmet","Yusuf");
How to Access Array Objects?Each value in the array has an address. These addresses in the arrays are called index by the programmer. The index (address) of the first value is expressed as 0. index and the value is accessed by writing the index numbers in square brackets. array[index];
Example:
var languages= ["CSS", "HTML", "JavaScript"];
document.write( languages[0] );//CSS will be written to the screen.
document.write(diller[1]);//HTML will be written to the screen.
How to change value in array?
To change the value in the array, the value is changed using the same method.
var languages= ["CSS", "HTML", "JavaScript"];
array[2]="Jquery";
Changed javascript to jquery.
In JavaScript, arrays are defined as objects. It is possible to find out the type of the array with the typeof method.
It is also possible to add Object, function or another array within the array.
Some methods and properties used in arrays
length property: used to find out the length of the array.
var languages = ["CSS", "HTML", "JavaScript"];
diller.length;//will return the value 3.
Adding objects to the array:
var languages = ["CSS", "HTML", "JavaScript"];
diller.push("Jquery");//adds the jquery value to the array.
To add value to the array with the length property;
var languages = ["CSS", "HTML", "JavaScript"];
languages[diller.length]="Jquery";
It is also possible to give an unordered index number and add values.
var languages = ["CSS", "HTML", "JavaScript"];
languages[15]="Jquery";