All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Declare Array In JavaScript

Last Updated : Mar 11, 2024

How To Declare Array In JavaScript

In this tutorial we will show you the solution of how to declare array in JavaScript, an array in JavaScript is a global object that holds a list of elements.

It's similar to other variables in that they can hold any form of data as long as the data type declaration is followed, but the distinction is that an array can hold multiple items at once.

An array can be declared in a variety of ways in JavaScript. 'Using array function Object() { [native code] }' and 'Using literal notation' are the most significant and common of them.

In this guide, we will learn about how to declare an array in JavaScript.

Step By Step Guide On How To Declare Array In JavaScript :-

Arrays aren't primitives in JavaScript; instead, they're Array objects with the following essential characteristics:

  • JavaScript arrays can be resized and hold a variety of data kinds.
  • Because JavaScript arrays are not associative, array items cannot be accessed using strings as indexes; instead, integers must be used.
  • JavaScript arrays are zero-indexed: the first element of an array is at index 0, the second is at index 1, and so on — and the last element is at the value of the array's length property minus 1.
  • JavaScript array-copy operations create shallow copies. (All standard built-in copy operations with any JavaScript objects create shallow copies, rather than deep copies).

The following is the code in which we have declared an array:

<!DOCTYPE html>
var emp=["Mudit","Suresh","Nitesh"];
for (i=0;i<emp.length;i++){
document.write(emp[i] + "<br/>");
  1. To begin, type <! DOCTYPE html> to tell the web browser that the file is in HTML format.
  2. The < html> element, on the other hand, is used to signal the start of HTML content.
  3. The <head> tag now contains the information about web pages. The <title> element is used in this tag to provide a web page title. For example, the < head> and <title> tags are paired tags. As a result, both have the </head> and </title> closing tags.
  4. Finally, the< body> element specifies the content of the web page. This is where all of the website's material will be written. We used the script tag inside the body element to incorporate our javascript code. We've allocated the values contained inside [ ] and separated by to the var employee (comma). We have used the if condition to iteerate the values in our array.
  5. Multiple elements of different data kinds can be stored in a JavaScript array. In an array, it is not mandatory to store values of the same data type. The length attribute returns the array's length. With the help of the document.write, we have printed our array.
  6. Finally <body> and <html> tags are closed with </body> and </html>, respectively.

Conclusion :-

An array is a variable that uses a particular syntax to store multiple values. It also has a length property and a number of methods for working with array objects. I hope this tutorial on how to declare array in JavaScript helps you.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪