Have you worked on Web Development? Do you want to know what Array inJavaScript is?
Then you have come to the right place. This article is focused on an important concept known as Array in JavaScript. We will learn the need of an array in JavaScript. We will also learn how to create and modify an array. We will learn about property and function as well.. Let's dive into the article to learn more about the topic.
In JavaScript, an array is a special variable that has multiple values. It means we can store more than one value in an array. We can access the values based on the index.
Example:
const languages = ["Java", "C", "Python"];
console.log(languages);
You can also try this code with Online Javascript Compiler
In any programming language, a variable can store a single value. You can't store more than one value in a single variable. You must create multiple variables if you want to store or modify a list of items.
For example,
let lang1 = "Java";
let lang2 = "C";
let lang3 = "Python";
You can also try this code with Online Javascript Compiler
Assume you want to store more than 400 or 500 values; what would you do then? Store them in individual variables?
The answer is NO; you don't have to. You can use an array to store multiple values in a single variable. You can use the index of the array to access a value.
Create an Array
In JavaScript, creating an array is relatively easy. You can create an array by two methods:
The first method, i.e., using the "array literal," is the recommended one. Sometimes, the new keyword produces unexpected outputs. Let's understand it with an example.
We will insert three elements in an array.
const prime = new Array(5, 7, 11);
console.log(prime);
You can also try this code with Online Javascript Compiler
Objects: When you want "strings" as the name of the elements.
Arrays: When you want "numbers" as the name of the elements.
The Elements can be Objects.
In JavaScript, the variables can be objects. We know that arrays are also objects. As a result, you can have variables of various types in the same Array.
An array is capable of holding items. An array is capable of containing functions. Even arrays are possible within an array.
Properties and Methods of an Array
You can create and use an array in JavaScript as per your need. But, there are many predefined array properties and methods. You can use the properties and methods to ease your work.
Example:
const languages = ["Java", "C", "JavaScript"];
// It will return the length of the array.
console.log(languages.length);
// It will return the sorted array.
languages.sort()
console.log(languages);
You can also try this code with Online Javascript Compiler
The length property is an essential property of an array. You can use it to return the length of the array. Length is the number of elements present in the array.
Example:
const languages = ["Java", "C", "JavaScript","Python"];
// It will return the length of the array.
console.log(languages.length);
You can also try this code with Online Javascript Compiler
You can use the Array.forEach() function to traverse through the array's elements via a function. It calls a function for every element present in the array.
Example:
const languages = ["Java", "C", "JavaScript"];
languages.forEach(myTestFunction);
function myTestFunction(value) {
console.log(value);
}
You can also try this code with Online Javascript Compiler
The print() function helps you to print the currently displayed contents, such as a web page, text, or image on the computer screen.
What is the console in JavaScript?
In JavaScript, a console is an object which gives access to the browser debugging console.
How can you merge two arrays in JavaScript?
The concat() method connects (concatenates) two or more arrays together. The concat() function returns a new array. The combined arrays are contained in the new array.
How to order an array in JavaScript?
To sort an array, use the JavaScript sort() method. It sorts the array in ascending order. It takes an array as an argument. It changes the original array. It does not create a new array.
What is a string in JavaScript?
You can use a string to store and modify text. A JavaScript string is any number of characters enclosed in quotations.
Conclusion
So that's the end of the article.
After reading about the Array in JavaScript, Are you interested in reading/exploring more themes on JavaScript? Don't worry; Coding Ninjas has you covered.