Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

JavaScript Arrays

By Exforsys | on June 7, 2007 |
JavaScript Tutorial

JavaScript Arrays

In this JavaScript tutorial, you will learn about JavaScript Array, different ways of defining an array in JavaScript viz. traditional way, shorter form, literal array, accessing the elements in an array and editing the values or elements in an array.

The array concept is used to store a set of values in a single variable name. This is a very important concept in any programming language. In this section, how to define and handle arrays in JavaScript is examined.

In order to work with the concept of arrays in JavaScript, there are many sections the programmer must become familiar with.

  • Array Definition
  • Accessing the elements in an array
  • Editing the values or elements in an array.
  • Single Dimensional Array
  • Two Dimensional Arrays
  • Different Operations with Array

Array Definition:

The array must first be defined before it is accessed or used. There are a variety of ways this can be accomplished in JavaScript. Below are some of the ways of defining arrays in JavaScript. Depending on convenience, the programmer can choose from any of the following techniques to define arrays in JavaScript:

Defining an Array in JavaScript:

Array is defined in JavaScript by making use of the keyword new.

General format of defining array in JavaScript is as follows:


var varaiblename = new Array( )

For example, if a programmer wants to define an array exforsys, it is written as follows:


var exforsys = new Array( )

Values or elements can be added in various ways to the above-defined array:

Traditional way:

This is the traditional way of adding elements to array in JavaScript. The first element of the array in JavaScript always starts with an index as zero.

The format for adding elements in this structure is as follows:


var varaiblename = new Array( )
variablename[0]=”element1”
variablename[1]=”
element2”
variablename[2]=”
element3”
variablename[3]=”
element4”
………………………
………………………

For example, if the programmer wants to store 4 elements (“Training”, “Division”, “Institute”, “Company” ) to the array named Exforsys, it is written as follows:


var Exforsys = new Array( )
Exforsys[0]=”Training”
Exforsys[1]=”
Division”
Exforsys[2]=”
Institute”
Exforsys[3]=”
Company”

It is also possible that if the programmer is certain of the array length, it can be defined while defining the array itself as:


var Exforsys = new Array(4)

Note that in the above example, since the value of the elements are strings, they are defined inside the double quotes. If the elements to be stored are integers, then they are placed without the double quotes. For example, if one wants to store 15 in array Exforsys it is written as follows:


var Exforsys = new Array( )
Exforsys[0]=15

Shorter form:

The elements can also be defined in a shorter form as:


 var variablename=new Array(“element1”,”element2”,”elements3”,….)

 

The same example used above (the array named Exforsys containing 4 elements) can be represented in a shorter form as: By this method of representation, the definition of the array and the defining of the elements in the array is written in a single step.


var Exforsys=new Array(”Training”,”Division”,”Institute”,”Company”)

 

Accessing the elements in an array:

An element in an array can be accessed by referring to the name of the array, followed by open braces [inside which the index number of the element to be accessed is placed and then the braces are closed using].

The index of an array in JavaScript starts with 0.

For example,

if a programmer wants to write the element “Institute” define in the array exforsys below.

It is written as follows:


var Exforsys=new Array( )

Exforsys[0]=”
Training”
Exforsys[1]=”
Division”
Exforsys[2]=”
Institute”
Exforsys[3]=”
Company”

document.write(exforsys[2])

will write Institute as output.

 

 

 

Editing the values or elements in an array:

It is also possible that the value or elements of the array defined can be edited or modified.

This is written using the same approach for accessing array elements.

For example,

if a programmer wants to change the value of “Division” to “Output” it can simply be written as follows:


Exforsys[1]=”Output”

Literal Array:

General format for defining the literal array is as follows:


var variablename=[“element1”,”element2”,…….]

For example, if a programmer wants to define an array named exforsys with the first element “Example”, the second and third elements of the array as undefined and the fourth element as “Training”, it can be written as follows:


var Exforsys=[“Example”, , , “Training”]

Thus, using this method, the programmer has the flexibility to leave elements undefined between the arrays.

« « Why Speaking Proper English is Important for your Career
Supply Chain Distribution Management » »

Author Description

Avatar

Editorial Team at Exforsys is a team of IT Consulting and Training team led by Chandra Vennapoosa.

Free Training

RSSSubscribe 394 Followers
  • Popular
  • Recent
  • JavaScript Exception Handling – Part II

    August 2, 2007 - 0 Comment
  • JavaScript Windows Object Properties Part II

    August 9, 2007 - 0 Comment
  • JavaScript Math Object

    August 9, 2007 - 0 Comment
  • JavaScript Object Oriented Features

    May 20, 2007 - 0 Comment
  • JavaScript OnError Event

    August 4, 2007 - 0 Comment
  • JavaScript Document Object

    July 29, 2007 - 0 Comment
  • JavaScript Iterative Structures – Part I

    July 26, 2007 - 0 Comment
  • JavaScript Browser Objects

    June 13, 2007 - 0 Comment
  • JavaScript Boolean Object

    June 7, 2007 - 0 Comment
  • JavaScript Variables

    April 15, 2007 - 0 Comment
  • JavaScript Objects

    August 12, 2007 - 0 Comment
  • JavaScript String Object

    August 12, 2007 - 0 Comment
  • JavaScript Date Object

    August 12, 2007 - 0 Comment
  • JavaScript Math Object

    August 9, 2007 - 0 Comment
  • JavaScript Windows Object Properties Part II

    August 9, 2007 - 0 Comment
  • JavaScript Windows Object Properties Part I

    August 8, 2007 - 0 Comment
  • JavaScript Window Object Timeout Methods

    August 8, 2007 - 0 Comment
  • JavaScript Document Object Methods Part II

    August 7, 2007 - 0 Comment
  • JavaScript Document Object Methods Part I

    August 5, 2007 - 0 Comment
  • JavaScript History Object Properties and Methods

    August 5, 2007 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • JavaScript Objects
  • JavaScript String Object
  • JavaScript Date Object
  • JavaScript Math Object
  • JavaScript Windows Object Properties Part II

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT