MyBlogHelp– A Blog for Digital Marketing, Affiliate Marketing, Organic Lead Generation.
  • Home
  • About
  • Contact
  • Privacy
  • Sitemap
  • BLOG
  • AFFILIATE MARKETING
    • LeadsArk
  • MORE
    • MAKE MONEY
      • ONLINE BUSINESS
      • ADSENSE
      • AFFILIATE MARKETING
    • BLOGGING
      • SEO
      • BlogSpot
      • WORPRESS
      • GOOGLE
      • COMPUTER TIPS
    • WEB DESIGN
      • HTML
      • CSS
      • BOOTSTRAP
      • JAVASCRIPT
      • JQUERY
    • WEB DESIGN
      • HTML
      • CSS
      • BOOTSTRAP
      • JAVASCRIPT
      • JQUERY
    • WEB DEVELOPMENT
      • PHP
      • WORDPRESS
  • DOWNLOADS
    • Blogger Template
    • Wordpress Theme
    • PDF

Friday, August 9, 2019

How to use array object in JavaScript?

 MyBlogHelp     August 09, 2019     javascript     2 comments   

array in javascript

 

   Array Object in JavaScript

  1. Introduction to JavaScript array
  2. Creating a JavaScript array
  3. JavaScript array object constructor


Introduction to JavaScript Array


An array is a collection of the same type of values in JavaScript. A same type of value means either only strings or integers or floating-point numbers etc. The JavaScript array is used to avoid the problem of creating many variables. 

For example, if you want to store 200 employees, you do not need to create 200 variables. You can create an array in which 200 names can be stored. By doing so, programmers save time.

Array Elements Indexing


Every element of the array can be uniquely identified. Array stores values ​​by indexing. But always remember this thing that the array starts index with zero. Therefore, in the above example, the first name will be on the zero indexes and the last name will be on 199 index. Any value of an array can be accessed by the name of the array and its index number. You should always remember that the arrays in JavaScript are the object. So you create them by using the new keyword. 

Although JavaScript also gives you the option of putting values ​​directly, but still there is an array object in JavaScript.

Creating JavaScript Arrays


An array can be created by two ways in JavaScript. Both of these methods can be used according to a different situation.

  • Directly - In this way, you can create the array and put the value as well. These are the combined way of creating an array.
  • With new keyword - In this manner, you create an array as an object. Where first of all, an array is created and thereafter values ​​can be inserted. If you want, you can put values at the same time, for that you will have to use the constructor of the array object.
Both methods can be used in different situations according to need. These are being given below.

Directly (Combined Way)


Creating an array in this manner is very simple. Its structure is given below.

Syntax

var array_Name = [value1, value2, ..... valueN];

In this method, the name of the array is written after the var keyword. Array's name should be unique. After the name of the array, the assignment takes place and thereafter values of an array will be in angular brackets. Values ​​are separated from the comma.

An example of this is being given below.

Example1: Creating & initializing array


<!DOCTYPE html>
<html>
<head>
 <title> JavaScript Array Example </title>
</head>
<body>
<script type="text/Javascript">
// Array declaration
var empName=["John","Jerry","Thomos","Juliya","Devid","Morgan","Tailor"];
// Printing array elements
for (i=0;i<7;i++)
{
     document.write(empName[i], "\n");
}
</script>  
</body>
</html>

The above script will generate the below the web page.

javascript array example

With New Keyword 


In this method, you can use the new keyword to create an array in javascript. This is similar to creating an object. Its structure is being given below.

Syntax

var array_Name = new Array ();
array_Name [0] = value1;
array_Name [1] = value2;
...
...
array_Name [n] = valueN;

If you want to put values ​​simultaneously then you can create an array from the below structure.

var array_Name = new Array (value1, value2);

In the above method, the first array has been created, followed by values ​​given by index numbers. Values ​​have been put in the following way by the constructor of the JavaScript array of objects. These assign values ​​to constructor automatic index numbers.

Example2: New keyword in array


<!DOCTYPE html>
<html>
<head>
 <title> JavaScript New Keyword Example </title>
</head>
<body>
<script type="text/javascript">
// Array declaration using new keyword
var empName=new Array ("Mariya","Adom","Pitar","Michal","William");
// Printing array elements
for (i=0;i<5; i++)
{
    document.write(empName[i],"\n");
}
</script>  
</body>
</html>


The above script will generate the below the web page.


New keyword in array example


JavaScript Array Length Property


JavaScript array of objects provides you length property. This property holds the length of your array. The structure of access to this property is given below.

Syntax

array_Name.length;

If you wish, you can use this property to control the loop. An example of this is being given below.

Example3: Loop to the array length


<!DOCTYPE html>
<html>
<head>
 <title> Javascript Array Length Example </title>
</head>
<body>
<script type="text/javascript">
var empAge = new Array (22,26,44,24,30,25,40,50,55,60); 
// Displaying length of array
document.write ("Length of array is:","\n" + empAge.length,"\n"); 

 document.write (", Arrays are:");
// Iterating loop upto array length
for (var i=0; i<empAge.length; i++)
{
    document.write ("\n" + empAge[i],"\n");
}
</script>
</body>
</html>

The above script will generate below the web page.


JavaScript Array Length example


    <<-- PREVIOUS                                                                         NEXT -->>












  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

2 comments:

  1. ranaAugust 10, 2019 at 6:04 PM

    This is a very amazing post for cheap web hosting services. in this post, you have provided all the basic information regarding.

    website builder for reseller

    ReplyDelete
    Replies
      Reply
  2. Banu RaviApril 4, 2020 at 2:11 PM

    Awesome blog with lots of information. It is really helpful for all.
    AngularJS Training in Chennai
    AngularJS course in Chennai
    Angular 6 Training in Chennai
    Angular Training in Chennai
    React JS Training in Chennai
    PHP Training in Chennai
    Web Designing course in Chennai
    Tally course in Chennai
    AngularJS Training in Velachery
    AngularJS Training center in Chennai
    Angular 7 Training in Chennai
    AngularJS Interview Questions

    ReplyDelete
    Replies
      Reply
Add comment
Load more...

Thank you for reading this post. Please do not enter any spam link in the comment box.

Followers

Blog Archive

  • ►  2020 (4)
    • ►  October (1)
    • ►  September (1)
    • ►  August (2)
  • ▼  2019 (52)
    • ►  December (1)
    • ►  November (2)
    • ►  October (1)
    • ▼  August (1)
      • How to use array object in JavaScript?
    • ►  July (1)
    • ►  May (11)
    • ►  April (10)
    • ►  March (6)
    • ►  February (3)
    • ►  January (16)
  • ►  2018 (54)
    • ►  December (27)
    • ►  November (5)
    • ►  May (5)
    • ►  April (4)
    • ►  February (3)
    • ►  January (10)
  • ►  2017 (17)
    • ►  December (9)
    • ►  November (8)

Popular Posts

  • How to use the main tag in html5?
  • How to use keygen tag in html5?
  • How to use html5 mark tag to highlight text?
  • How to start Online Business without money from home?
  • What is a search engine?

Contact Us

Name

Email *

Message *

About MyBlogHelp

Most of the posts, we share on this blog related to Affiliate Marketing, Organic Lead Generation, Digital Marketing, Social Media Marketing, Make Money Online, SEO, and web design.

Featured Post

LeadArk - Affiliate Marketing | Qualified Organic Lead Generation | LeadsArk Review

Copyright © MyBlogHelp– A Blog for Digital Marketing, Affiliate Marketing, Organic Lead Generation.
Design by Md. Abdussamad | MyBlogHelp.com