Tuesday, December 28, 2021
Arrays in JavaScripts are a collection of values stored in what are called elements. Each element is represented by a numeric index that is zero based, meaning the first element of an array starts at index zero. JavaScript arrays are dynamic and can contain many types of objects including functions. The useful thing about arrays is that they are a specialized object that contains a variety of built-in methods that are useful in the manipulation of the array.
First let's create an empty array, there are several ways to do this:
Now let's do something that only JavaScript can do very easily. Let's create a mix array of the three above examples. Let's call it mixedArray, like the code below:
First let's create an empty array, there are several ways to do this:
- var empty = [ ];
- var empty = new Array();
- var empty = new Array(0);
The first example creates an empty array using the [ ] syntax, the second example creates an empty array by instantiating a new Array object with it's default constructor, then the third example creates an array with the Array constructor passing in the element argument of zero. If you output the array with console.log(empty) they would all be the same output, an array with zero elements
Now let's create arrays with different types of data:
- var numbers = [1,2,3,4];
- var stringArray = new Array("USA", "Canada","China");
- var objectArray = {firstName: "Jack", lastName:"Johnson", weight:"200lbs"};
The first example is an array of numbers, the second array is an array of strings, and the the third array is an array of object. If we use the console.log method to output the arrays we will find that the first two arrays has a prototype object of Array, but the last array has a prototype object of Object
console.log(numbers);
console.log(stringArray);
console.log(objectArray);
If you look at the last array objectArray you will noticed that it's not an array at all its actually an object because it is using the prototype Object. That's because it is declared as an object with just the { } braces. To make it into an array you just need to surround it with the [ ] brackets. Like the example below:
var objectArray = [{firstName: "Jack", lastName:"Johnson", weight:"200lbs"}];
Now it's an array of objects
var objectArray = [{firstName: "Jack", lastName:"Johnson", weight:"200lbs"}];
Now it's an array of objects
Now let's do something that only JavaScript can do very easily. Let's create a mix array of the three above examples. Let's call it mixedArray, like the code below:
var mixedArray = [numbers, stringArray, objectArray];
If we output it in the console with console.log(mixedArray) we would see the we get all three arrays in mixedArray. This is what is called an array of arrays or associative arrays.
,
Subscribe to:
Post Comments (Atom)
Search This Blog
Tags
Web Development
Linux
Javascript
DATA
CentOS
ASPNET
SQL Server
Cloud Computing
ASP.NET Core
ASP.NET MVC
SQL
Virtualization
AWS
Database
ADO.NET
AngularJS
C#
CSS
EC2
Iaas
System Administrator
Azure
Computer Programming
JQuery
Coding
ASP.NET MVC 5
Entity Framework Core
Web Design
Infrastructure
Networking
Visual Studio
Errors
T-SQL
Ubuntu
Stored Procedures
ACME Bank
Bootstrap
Computer Networking
Entity Framework
Load Balancer
MongoDB
NoSQL
Node.js
Oracle
VirtualBox
Container
Docker
Fedora
Java
Source Control
git
ExpressJS
MySQL
NuGet
Blogger
Blogging
Bower.js
Data Science
JSON
JavaEE
Web Api
DBMS
DevOps
HTML5
MVC
SPA
Storage
github
AJAX
Big Data
Design Pattern
Eclipse IDE
Elastic IP
GIMP
Graphics Design
Heroku
Linux Mint
Postman
R
SSL
Security
Visual Studio Code
ASP.NET MVC 4
CLI
Linux Commands
Powershell
Python
Server
Software Development
Subnets
Telerik
VPC
Windows Server 2016
angular-seed
font-awesome
log4net
servlets
tomcat
AWS CloudWatch
Active Directory
Angular
Blockchain
Collections
Compatibility
Cryptocurrency
DIgital Life
DNS
Downloads
Google Blogger
Google Chrome
Google Fonts
Hadoop
IAM
KnockoutJS
LINQ
Linux Performance
Logging
Mobile-First
Open Source
Prototype
R Programming
Responsive
Route 53
S3
SELinux
Software
Unix
View
Web Forms
WildFly
XML
cshtml
githu
Hi, I really enjoyed reading your post, and hope to read more. Thank you so much for sharing this. call center interview questions and answers 2020
ReplyDeleteThere is always need of Essay writing services across the globe being the sociology student I was assigned Essay Assignment which had a very short deadline so I needed someone to Do my Essay in order to do the same I needed Essay help from the experts I tried for Greatassignmenthelper.com they are great in their high quality content and on time submission.
ReplyDelete