TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVASCRIPT

JavaScript Initialize Array

Initialize arrays with literal syntax and the Array constructor.
Initialize array. In programs we want to fill an array immediately with certain values. In JavaScript many syntax forms for array initialization are available.Array
For the simplest code, using an array literal to start is a good plan. We can have an empty array literal, or one with many elements in it.
Literal example. Here is an integer array with 3 initial elements. The initialization statement is clear and does not require many extra characters. This is ideal.
JavaScript program that uses array literal // Initialize array with 3 elements. var values = [10, 20, 30]; console.log("ARRAY: " + values); Output ARRAY: 10,20,30
Constructor. We can use the Array constructor with the new keyword in JavaScript. This is more complex. With one argument, we have a specified number of empty elements in our array.

Warning: With one argument, we do not get a one-element Array. This specifies a capacity or initial length.

Performance: In a performance test, I found that an initial size can reduce array resizes and lead to better program performance.

JavaScript program that uses constructor, 1 argument // Create array with 3 empty elements. // ... Then assign them with indexes. var values = new Array(3); values[0] = 10; values[1] = 20; values[2] = 30; console.log("EXAMPLE: " + values); Output EXAMPLE: 10,20,30
Constructor, many arguments. With two or more arguments, the Array constructor returns an array with those elements. This is the same as the array literal syntax.
JavaScript program that uses constructor, 3 arguments // Use array constructor with 2 or more arguments to create an array. var values = new Array(10, 20, 30); console.log("VALUES: " + values); Output VALUES: 10,20,30
Constructor, no new. The "new" keyword is not required with the Array constructor. Google's Closure Compiler removes the "new" keyword as it is not required and makes the code longer.
JavaScript program that uses Array constructor, no new // The new keyword is not required. // ... It is removed by Closure Compiler. var result = Array(3); console.log("RESULT: " + result); Output RESULT: ,,
Empty literal. This is a good approach to creating an array. We create an empty array with an empty array literal. Then we push elements to it—we can even do this in a loop.for

Sometimes: We do not know all the elements immediately. This approach allows us to add elements as soon as we know what they are.

JavaScript program that uses empty array, push // Create an empty array and push elements to it. var values = []; values.push(10); values.push(20); values.push(30); console.log("ELEMENTS: " + values); Output ELEMENTS: 10,20,30
Unshift. This is the worst approach to building an array. With unshift we add to the front of an array. But this is slow because after each call, all following elements are affected.
JavaScript program that uses empty array, unshift // Use unshift to initialize an array in reverse order. var values = []; values.unshift(30); values.unshift(20); values.unshift(10); console.log("RESULT: " + values); Output RESULT: 10,20,30
A review. With square brackets, we create empty arrays and arrays with certain elements. This is an ideal syntax for initializing integer and string arrays in JavaScript.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf