DEV Community

Arya Krishna
Arya Krishna

Posted on

4 3

Arrays in Javascript

Let us see how we can do the following in javascript -

  • How to store collections of values in arrays

  • How to loop over anarray to apply repetitive code to each value in a collection

  • How to create and call functions to execute sequel blocks of code on demand

  • How to pass values into functions to create dynamic results

We have been storing one piece of data in variables

var name = "Arya";
var books = 4; 
var isStudent = true;
Enter fullscreen mode Exit fullscreen mode

Arrays are used to store groups of data in a single variable.

var names = ["Arya", "Krishna", "Manvi", "Amogh"];
Enter fullscreen mode Exit fullscreen mode

We name arrays in the same way as we did for a single variable. We name a variable, use the assignment operator and assign a value to the array list. The key syntax or the way we assign the value to an array is that we type a set of []as a value and with coma separating values we write each elements in an array list.

In the above example of var names, we have created a sequence collection of 4 string values inside square brackets.

Remember that arrays are an ordered list means all the elements are assigned a position.

Arrays are zero-indexed, so the index of first element in the array is 0.
That means when we are trying to assign a position in an array, we count up from zero.

If we have to go and access a specific value, then we will go ahead and use the variable name again and extending it with square brackets and using the index pointer to point towards a specific item in the array.
That means to log a single element, we use the name of the array with the index in brackets

console.log(names[1]); 
Enter fullscreen mode Exit fullscreen mode

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

Cloudinary image

Zoom pan, gen fill, restore, overlay, upscale, crop, resize...

Chain advanced transformations through a set of image and video APIs while optimizing assets by 90%.

Explore

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay