Js Array


What is JavaScript?

Javascript is the world's most popular programming language. It is easy to learn a web programming language.

It is one of the 3 languages all web developers must learn:

  •    HTML: defines the content of web pages
  •    CSS: specifies the layout and styling of web pages
  •    JavaScript: responsible for adding behaviour to web pages

What are Arrays in Javascript?

An array is a particular variable that can hold multiple homogenous values simultaneously. These are powerful and comprehensive tools of Javascript, and they are very intuitive to use, and you can do almost everything with them. However, there are essential differences between arrays in Javascript and other mainstream languages.

In Javascript, there are only 6 data types defined – the primitives (boolean, number, string, null, undefined) and object (the only reference type). Arrays do not belong to this list because they are objects as well. This is a common confusion among developers who assume that arrays are a particular data type in Javascript.

The items in the Array are nothing more than the properties of that objects. You can use any name as a property for an object, including numbers or even empty strings. However, the property name should be a valid identifier.

For Creating an array:
 const array_name = [item1, item2, ...];
       Or
Using “new” keyword
const arr = new Array("A", "B", "C");

There is no need to use "new" Array (). For simplicity, readability and execution speed, use the first method to create an array.

Example No.1:

<html> <body> <h4>Knowledge2life</h4> <p id="demo"></p> <script> const web = ["Knowledge2life 1", " Knowledge2life 2"]; document.getElementById("demo").innerHTML = web; </script> </body> </html>

OUTPUT:

Knowledge2life

Knowledge2life 1,Knowledge2life 2

Example No.2:

<html> <body> <h2>Knowledge2life</h2> <p id="demo"></p> <script> const web = [ "website", "online", "Learning" ]; document.getElementById("demo").innerHTML = web; </script> </body> </html>

OUTPUT:

Knowledge2life

website,online,Learning