Friday , 17 November 2017
Home / Programming / JavaScript / Multidimensional Arrays – JavaScript Tutorial
Multidimensional Arrays
Multidimensional Arrays

Multidimensional Arrays – JavaScript Tutorial

This tutorial is part of the JavaScript tutorial series for beginners. To view the whole list of tutorials available and the order in which you should follow them, then click here.

When you hear the word multidimensional you think of space and time. In programming, it’s far more simpler than that. Multidimensional arrays are just arrays within arrays. Yes, it’s possible! Don’t worry, this tutorial doesn’t have any surprised ;).

Let’s clear your script and add this bit of code into it.

I create a variable named people and insert some regular values. Don’t worry about the formatting. You don’t have to put every value on a new line, but I do this to make it more readable.

The last value is an array.To create an array inside an array, you would do it just like any other array. You can input as many values as you want inside this array. You can even continue on with your array after your finished with the inner array.

Now, add this bit of code after your array.

This will simply output all values in the array. Since you’re not defining to access a certain element, it’ll assume you want all the values. It’ll work whether or not you’re using multidimensional arrays.

So, you may be wondering, what’s the difference between this and the join() method? Well, the join() method allows you to define a separator instead of using commas. Other than that, they do the same thing.

So, how do you access values that are inside an array? Simple, add this line of code.

First, you specify the index that contains the array, then you type in another square brackets. The second pair of brackets will represent the 2nd array. The index will restart at 0. So, the index 1, will be the value Karen.

You can even create an array inside your array inside your array. You would have a 3 dimensional array. The more in depth you go, then the more complicated it can get. It’s very rare to have a 3 dimension array. A 2 dimension array is usually as far as you’ll go.

Conclusion

Multidimensional arrays are used from time to time. They allow you to organize data more efficiently. There are other ways to do so, which we’ll learn in future tutorials.

About Jasko Koyn

Check Also

Try-Catch Statement

Try-catch Statement – Advanced JavaScript Tutorial

The try-catch statement is a unique way to handle errors. When errors occur, usually it …

Leave a Reply

Your email address will not be published. Required fields are marked *

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link