Saturday, February 29, 2020

How to implement JavaScript String Properties with clear examples


Hello guys evening to you all, i want to welcome you to this new tutorial section. In my previous tutorial post we discussed about JavaScript String Object and we listed down the various String properties and also String methods available.

In this tutorial, am going to use few examples to illustrate how the String property can be used. Make sure you read through very carefully and feel free to ask your questions.

String - constructor Property

A constructor returns a reference to the string function that created the instance's prototype.

Syntax

Its syntax is as follows -

string.constructor

Return Value

Returns the function that created this object's instance.

Example

Try the following example below.

<html>
     <head>
          <title>JavaScript String constructor Method</title>
     </head>

     <body>
          <script  type = "text/Javascript">
               var str = new String("This is a string");
               document.write("str.constructor is : " + str.constructor );       
          </script>
     </body>
</html> 

Output

Below is the output of the above example

str.constructor is : function string()  { [native code] }

You can also read our tutorial post on: Html Elements


String - length Property

This property returns the number of characters in a string.

Syntax

Its syntax is as follows -

string.length

Return Value

Returns the number of characters in a string.

Example

Try the following example below.

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

     <body>
          <script  type = "text/Javascript">
               var str = new String("This is a string");
               document.write("str.length is : " + str.length );       
          </script>
     </body>
</html> 

Output

Below is the output of the above example

str.length is : 13

You can also read our tutorial post on: Html-Comments


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 = 400;
               
               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 : 400


Alright guys, we have come to the end of this tutorial post. In my next tutorial post i will be using few examples to demonstrate the usage of String methods.

Follow us on our various social media handles to stay updated with our latest tutorials, you can also subscribe to our newsletter in other to get our tutorials delivered directly to your emails. Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment