Different ways to write arrays in javascript

Explore our JavaScript category, where you’ll find loads of helpful articles and tutorials for everyone, whether you’re just starting out or a pro developer. Learn about different JavaScript functions that can be used in various projects according to their specific requirements.
JavaScript script that creates and initializes three arrays (array1, empty, and thirdArray) and then logs them to the console. Here’s a breakdown of your code:
<script>
// Creating and initializing array1
var array1 = ["string-1", "string-2", "string-3"];
// Creating an empty array called empty and adding elements to it
var empty = [];
empty[0] = "empty1";
empty[1] = "empty2";
// Creating and initializing thirdArray using the Array constructor
var thirdArray = new Array("st-1", "st-2", "st-3");
// Logging the contents of the arrays to the console
console.log(array1);
console.log(empty);
console.log(thirdArray);
</script>
When you run this script in an HTML document and open the browser console, you will see the three arrays printed with their respective elements. The output will look something like this:
["string-1", "string-2", "string-3"]
["empty1", "empty2"]
["st-1", "st-2", "st-3"]
This script demonstrates different ways to create and initialize arrays in JavaScript. The arrays contain strings as elements, and the code logs them to the console for inspection.