Introduction
An array is one of the most commonly used data structures in JavaScript. It is used to store multiple values in a single variable, making it easier to manage and process collections of related data.
Instead of creating separate variables for each value, you can store all the values in a single array and access them using their index positions.
Arrays are widely used in web development, Node.js applications, API testing, and automation frameworks such as Selenium, Playwright, and Cypress. Automation engineers frequently use arrays to store test data, browser names, URLs, user credentials, API responses, and lists of web elements.
What is an Array?
An array is a special JavaScript object that stores multiple values in an ordered collection.
Each value inside an array is called an element, and every element has an index.
The first element is stored at index 0, the second at index 1, and so on.
Example:
Array: ["Chrome", "Firefox", "Edge"]
Index: 0 1 2
Why Do We Use Arrays?
Arrays help developers:
Store multiple values together.
Organize related data.
Reduce the number of variables.
Easily loop through data.
Simplify data processing.
Improve code readability.
Creating an Array Using Square Brackets
The most common way to create an array is by using square brackets ([]).
Syntax
let arrayName = [value1, value2, value3];
Example
let browsers = ["Chrome", "Firefox", "Edge"];
console.log(browsers);
Output
[ 'Chrome', 'Firefox', 'Edge' ]
Creating an Empty Array
You can create an empty array and add elements later.
let users = [];
console.log(users);
Output
[]
Creating an Array with Numbers
let marks = [85, 90, 95, 88];
console.log(marks);
Output
[ 85, 90, 95, 88 ]
Creating an Array with Strings
let cities = ["Delhi", "Mumbai", "Bengaluru"];
console.log(cities);
Output
[ 'Delhi', 'Mumbai', 'Bengaluru' ]
Creating an Array with Mixed Data Types
JavaScript arrays can store different types of values.
let employee = [
"John",
28,
true
];
console.log(employee);
Output
[ 'John', 28, true ]
Creating an Array Using the Array Constructor
Arrays can also be created using the Array constructor.
Syntax
let arrayName = new Array(value1, value2, value3);
Example
let fruits = new Array(
"Apple",
"Mango",
"Orange"
);
console.log(fruits);
Output
[ 'Apple', 'Mango', 'Orange' ]
Creating an Array from Variables
let browser1 = "Chrome";
let browser2 = "Firefox";
let browser3 = "Edge";
let browsers = [
browser1,
browser2,
browser3
];
console.log(browsers);
Output
[ 'Chrome', 'Firefox', 'Edge' ]
Real-World Example
Suppose an application supports multiple browsers.
let supportedBrowsers = [
"Chrome",
"Firefox",
"Edge",
"Safari"
];
console.log(supportedBrowsers);
Output
[ 'Chrome', 'Firefox', 'Edge', 'Safari' ]
Another example:
Store employee IDs.
let employeeIds = [
1001,
1002,
1003
];
console.log(employeeIds);
Output
[ 1001, 1002, 1003 ]
Automation Testing Example
Automation engineers frequently create arrays to store test data, browser names, URLs, and user credentials.
Playwright Example
Store browser names.
const browsers = [
"chromium",
"firefox",
"webkit"
];
console.log(browsers);
Selenium Example
Store application URLs.
const urls = [
"https://example.com",
"https://qa.example.com"
];
console.log(urls);
Cypress Example
Store page names.
const pages = [
"login",
"dashboard",
"profile"
];
console.log(pages);
API Testing Example
Store API endpoints.
const endpoints = [
"/users",
"/products",
"/orders"
];
console.log(endpoints);
Output
[ '/users', '/products', '/orders' ]
Data-Driven Testing Example
Store usernames.
const users = [
"admin",
"manager",
"tester"
];
console.log(users);
Output
[ 'admin', 'manager', 'tester' ]
Different Ways to Create Arrays
| Method | Example |
|---|---|
| Array Literal | let arr = [1, 2, 3]; |
| Empty Array | let arr = []; |
| Array Constructor | let arr = new Array(1, 2, 3); |
| From Variables | let arr = [a, b, c]; |
Common Mistakes
Using Curly Braces Instead of Square Brackets
Incorrect:
let fruits = {
"Apple",
"Orange"
};
Curly braces create an object, not an array.
Correct:
let fruits = [
"Apple",
"Orange"
];
Forgetting Commas Between Elements
Incorrect:
let colors = [
"Red"
"Green"
"Blue"
];
Each element must be separated by a comma.
Correct:
let colors = [
"Red",
"Green",
"Blue"
];
Using new Array() Unnecessarily
Instead of:
let numbers = new Array(
1,
2,
3
);
Prefer:
let numbers = [
1,
2,
3
];
Array literals are shorter, cleaner, and recommended.
Best Practices
Prefer Array Literals
Use square brackets ([]) instead of the Array constructor whenever possible.
Store Similar Data Together
Keep related values in the same array.
Example:
const browsers = [
"Chrome",
"Firefox",
"Edge"
];
Use Meaningful Variable Names
Instead of:
let a = [
"Chrome",
"Firefox"
];
Use:
let supportedBrowsers = [
"Chrome",
"Firefox"
];
This improves readability.
Keep Arrays Organized
Group similar values together instead of mixing unrelated data unless required.
Conclusion
Arrays are one of the most important data structures in JavaScript. They allow developers to store multiple values in a single variable, making programs more organized and easier to maintain. JavaScript provides several ways to create arrays, but the array literal ([]) is the most commonly used and recommended approach.
For automation engineers, arrays play a crucial role in storing test data, browser names, URLs, API endpoints, user credentials, and collections of web elements. Understanding how to create arrays is the foundation for learning array access, array methods, iteration, filtering, and other advanced JavaScript concepts.
Frequently Asked Questions (FAQs)
What is an array in JavaScript?
An array is an ordered collection that stores multiple values in a single variable.
What is the most common way to create an array?
Using square brackets.
let fruits = [
"Apple",
"Mango",
"Orange"
];
Can an array store different data types?
Yes. JavaScript arrays can store strings, numbers, booleans, objects, arrays, functions, and more.
Can I create an empty array?
Yes.
let users = [];
Which is better: [] or new Array()?
Using square brackets ([]) is recommended because it is simpler, more readable, and widely used.
Why are arrays important in automation testing?
Automation engineers use arrays to store test data, browser names, URLs, API endpoints, user credentials, expected results, and collections of web elements, making automation scripts more organized and reusable.
Key Takeaways
An array stores multiple values in a single variable.
Array indexing starts from
0.The array literal (
[]) is the preferred way to create arrays.Arrays can store different data types.
Arrays can be empty or contain any number of elements.
The
Arrayconstructor can also be used but is less common.Arrays improve code organization and readability.
They are widely used in JavaScript development and automation testing.
Use meaningful names for arrays.
Mastering array creation is the first step toward learning array access, iteration, and array methods.
