1.

Should I include type=“text/javascript” in my SCRIPT tags

Answer»

Arrays in JavaScript are used to store multiple VALUES in a single variable. Let us see the two ways to create an array in JavaScript:

Using Array Literal

The easiest way to create an array in JavaScript is with array literal. Here, we have a variable and we are setting multiple values to it in an array:

var CARS = ["Saab", "Volvo", "BMW"];

Let us see an EXAMPLE:

<!DOCTYPE html> <html> <body> <p id="myid"></p> <script> var department = ["HR", "FINANCE", "Operations", "Marketing"]; document.getElementById("myid").innerHTML = department; </script> </body> </html>

The output:

HR,Finance,Operations,Marketing

Using keyword new

With JavaScript, you can also create an array using the new operator:

var department = new Array("HR", "Finance", "Operations", "Marketing");

Let us see an example:

<!DOCTYPE html> <html> <body> <p id="myid"></p> <script> var department = new Array("HR", "Finance", "Operations", "Marketing"); document.getElementById("myid").innerHTML = department; </script> </body> </html>

The output:

HR,Finance,Operations,Marketing


Discussion

No Comment Found