Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning JavaScript Data Structures and Algorithms

You're reading from   Learning JavaScript Data Structures and Algorithms JavaScript Data Structures and algorithms can help you solve complex development problems – learn how by exploring a huge range of JavaScript data types

Arrow left icon
Product type Paperback
Published in Oct 2014
Publisher Packt
ISBN-13 9781783554874
Length 218 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Loiane Avancini Loiane Avancini
Author Profile Icon Loiane Avancini
Loiane Avancini
Arrow right icon
View More author details
Toc

Creating and initializing arrays

Declaring, creating, and initializing an array in JavaScript is as simple, as follows:

var daysOfWeek = new Array(); //{1}
var daysOfWeek = new Array(7); //{2}
var daysOfWeek = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'); //{3}

We can simply declare and instantiate a new array by using the keyword new (line {1}). Also, using the keyword new, we can create a new array specifying the length of the array (line {2}). And a third option would be passing the array elements directly to its constructor (line {3}).

However, using the new keyword is not a best practice. If you want to create an array in JavaScript, simply use brackets ([]) like in the following example:

var daysOfWeek = [];

We can also initialize the array with some elements, as follows:

var daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', &apos...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime