Skip to content
Advertisements

Javascript Array Creating

The array is the special variable in which similar expressions are combined.

When a data is stored in a variable, it is possible to store multiple data in arrays. In the first line of the sample code snippet below, an array called languages ​​is created. In the second line, the tag that is the idsi instance is selected and the values ​​in the array are placed.

<p id="example"></p>
    
        var language = ["html", "css", "javascript"];
        document.getElementById("example").innerHTML = language;

If we didn’t use the array, we would have to write the values ​​above.

var lan1 = "html";
var lan2 = "css";
var lan3 = "javascript";

Creating an Array with the “New” Keyword

You will need to write the code in the following way with the “new” keyword in the create array.

var language = new Array("css","html","js")

How to Access Array Objects?

Each value in the array has an address. These addresses in the series are called “index” as programmers.

language[index]

example:

var language = ["css", "html", "js"];
document.write(language[0]);
document.write(language[1]);
document.write(language[2]);

How is the value in the array changed?

var language = ["css", "html", "js"];
language[1] = "php";//html ---> php

Some methods and properties used in arrays

length property: used to find the length of the array.

Add an object to the array:

var language = ["css", "html", "js"];
        language.push("php");//Method 1
        language[language.length] = "jquery"//Method 2
        language[13] = "sql"//Method 3
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: