Declaring arrays in javascript like a boss

Declaring arrays in javascript like a boss
Declaring arrays in javascript like a boss

What is an array in JavaScript?

A Javascript Array object lets you store multiple values in a single variable. Stores a fixed-size sequential set of items of the same type. declaring arrays in javascript is easy.

If you have a list of items (a list of cell phone tags, for example). storing the cell phone tag in individual variables. declaring arrays in javascript with cell phone tags.
The array can hold more than one value.
The array can contain many values under one name, and you can access the values by reference to the index number.

read more: 10 Top programming languages 2020

Declaring arrays in javascript

There are two ways to declare an array.
Example declaration :

Method 1

var hostingSite = [ ];

Method 2

var hostingSite = new array();

Method 1 is preferred over method 2. Let us understand the reason for this.

The two examples above do the same thing. There is no need to use a new Array ().
Use the first method to simplicity and speed of implementation.

Initialization of an Array

Declaring arrays in javascript like this

var hostingSite = ["bluehost", "hostgator", "a2hosting"];

Or this

var hostingSite = [
  "bluehost",
  "hostgator",
  "a2hosting"
];

It is the same thing. Spaces and line breaks are not important.

Access the Elements of an Array

The index number is necessary to access the array element.
This is an example of how to access the value of the first element in an array.
Where the index number is 0. The Array indexes start with 0.

var vale0 = hostingSite[0];  // bluehost

length of an array javascript

The Length property returns and sets the number of elements in an array.

Declaring arrays in javascript

var hostingSite = ["bluehost", "hostgator", "a2hosting"];
hostingSite.length;

Get the last element of array javascript

var hostingSite = ["bluehost", "hostgator", "a2hosting"];
var lastElementIndex = hostingSite.length;
var lastElement = hostingSite[lastElementIndex -1];

How to compare arrays in JavaScript?

Objects must be of the same type in arrays
We need to verify that the lengths of both arrays are the same when we need to compare two arrays.

By comparing an element with an element, we can conclude that both arrays are the same or not.

We use JSON.stringify () function to convert an array to a JSON string. then we check if the strings are equal.

var hostingSite1 = ["bluehost", "hostgator", "a2hosting"];
var hostingSite2 = ["bluehost", "hostgator", "a2hosting"];
// convert hostingSite1 to Json 
var json1= JSON.stringify(hostingSite1);
var json2= JSON.stringify(hostingSite2);
// comparing 
if(json1 ==json2) 
 document.write("True");  // if True 
else
 document.write("False"); // if False

javascript union of two arrays?

var x= ["bluehost", "a2hosting"];
var y= ["hostgator" ,"GoDaddy" ];
  var obj = {};
  for (var i = x.length-1; i >= 0; -- i)
     obj[x[i]] = x[i];
  for (var i = y.length-1; i >= 0; -- i)
     obj[y[i]] = y[i];
  var res = []
  for (var k in obj) {
    if (obj.hasOwnProperty(k))  // <-- optional
      res.push(obj[k]);
  }
  // result res = ["bluehost", "a2hosting" , "hostgator" ,"GoDaddy" ];

what is a function in javascript?

A function is a set of statements that perform certain tasks or perform some action and then return the result to the user. Take input, perform some specific calculations and produce output.

The idea is to group some common or repetitive tasks together and make a function so that we can call this function instead of typing the same code over and over for different inputs.

The basic syntax to create a function in JavaScript is shown below.

function nameFunction(Param1, Param2, ..)
{
    // body
}

Example arrayUnion :

function arrayUnion (x, y) {
  var obj = {};
  for (var i = x.length-1; i >= 0; -- i)
     obj[x[i]] = x[i];
  for (var i = y.length-1; i >= 0; -- i)
     obj[y[i]] = y[i];
  var res = []
  for (var k in obj) {
    if (obj.hasOwnProperty(k))  // <-- optional
      res.push(obj[k]);
  }
  
}

var x= ["bluehost", "a2hosting"];
var y= ["hostgator" ,"GoDaddy" ];

ShowMessage(x, y); // result res = ["bluehost", "a2hosting" , "hostgator" ,"GoDaddy" ];


javascript arrays sort

how to sort an array:

var hostingSite = ["bluehost", "hostgator", "a2hosting"];

hostingSite.sort();

The sort () function sorts the array elements, the javascript sort array.

An order that is alphabetical or numeric, ascending or descending.

By default, in ascending order.

Leave a Reply

Your email address will not be published. Required fields are marked *