Monday, March 30, 2020

The complete guide to JavaScript Array Properties with detailed examples



Hello everyone! evening to you all. I want to welcome you guys to my new tutorial post on JavaScript. In my last tutorial post, i introduced JavaScript Array Object to you guys. So am now am going to move onto Array Properties.

In my last tutorial post, i already introduced array properties without any actual example. So now i want to use few examples to illustrate how JavaScript array objects are implemented. So now am going to start with array constructor property.

Array constructor Property

JavaScript array constructor property returns a reference to the array function that created the instance's prototype.

Syntax

Its syntax is as follows -

array.constructor

Return Value

Returns the function that created this object's instance.

Example

Try the following example below.

<html>
     <head>
          <title>JavaScript Array constructor Property</title>
     </head>

     <body>
          <script  type ="text/javascript">
               var  arr = new Array ( 10,  20,  30 );
               document.write("array.constructor is:"  +  arr.constructor);      
          </script>
     </body>
</html>   

Output

Below is the output of the above example.

array.constructor is:  function  Array() {  [native code]  }

You can also read our tutorial post on: How to implement JavaScript Sthing Html Wrappers?


Array length Property

JavaScript array length property returns unsigned, 32-bit integer that specifies the number of elements in an array.

Syntax

It's syntax is as follows -

array.length

Return Value

Returns the length of the array.

Example

Try the following example below

<html>
     <head>
          <title>JavaScript Array length  Property</title>
     </head>

     <body>
          <script  type ="text/javascript">
               var  arr = new Array ( 10,  20,  30 );
               document.write("array.length is:"  +  arr.length);      
          </script>
     </body>
</html>   

Output

Below is the output of the above example.

array.length is:  3



Object - prototype
The prototype property allows you to add properties and methods to any object(Number, Boolean, String and Date etc.).

Note - Prototype is a global property which is available with almost all the objects.

Syntax
Its syntax is as follows -

object.prototype.name = value

Example
Try the following example below

<html>
     <head>
          <title>User-defined objects</title>
          <script type = "text/javascript">
               function  book(title,  author)  {
                    this.title = title; 
                    this.author = author; 
               }
          </script>
     </head>

     <body>
          <script  type ="text/javascript">
               var  myBook = new book("Java",   "Kennedy");
               book.prototype.price = null; 
               myBook.price = 300;

               document.write("Book title is : " + myBook.title + "<br />" );            
               document.write("Book author is : " + myBook.author + "<br />" );       
               document.write("Book price is : " + myBook.price + "<br />" );
          </script>
     </body>
</html>   

Output
Below is the output of the above example.

Book title is :  Java
Book author is :  Kennedy
Book price is :  300

Alright guys! we have come to the end of this tutorial on JavaScript array properties. In my next tutorial post, I will be making use of series of examples to demonstrate how to implement the array methods.

Feel free to drop your questions via the comment box below. You can also follow us on our various social media platforms available to stay updated with our latest tutorials.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.