Java Script Strings Step By Step Guide

Introduction to java script:

Java Script is a text based programming languages used for web development and in a letiety of other environments. It is an interpreted high level language and this can be embedded inside the HTML code. Each computer programming language has its own data types. Strings are useful for storing data in as text.Here we will see about java script.

java script.

These data types can be :

  • 1.Primitive

Primitive data types are the primary (elementary) data types. 

  • 2.Derived

 Derived data types are formed from the primitive data types.

 Strings are  derived data type in many programming languages since they are a collection of characters,the character data type is

 primitive. Primitive data types are used directly at the elementary level of a language.

Java script strings:

         In  Javascript the ‘string’ is a primitive data type. Primitive data types are immutable. That means you cannot modify it, whereas while modifying it, another string will be formed. Javascript strings are used for storing and performing certain operations. String can have 0 or more number of characters. The ‘length’  property is used to find the length of a string. Single characters from a string can be accessed by their corresponding index positions.

                      String methods are pre-built methods which can perform operations on strings with out writing code manually. 

                      They are executed using a dot notation to the string variable.

  • Either ‘single ‘quotes’ or ‘double quotes’ are used for declaring a string. 
  •  1.If you are starting with a single quote then you have to complete it with single quote itself. Similarly for double quote also.
  • 2.String comparison can be performed either using methods or by using character by character .

Format is as follows:

 Key word  string-name = ‘characters’;

      Key word  string-name = “characters”;

   Key word can be var, const or let used to declare a literal in a program.

Eg: 

       let myname1 = ‘My first tring’;

       let myname2 = “My first string”;

  • 1.Can be defined as String objects. The key word ‘new’ is used to create a string object.

Eg: 

     let myname1 = new String (“abcd”);

The part of the declaration of a string in the program will look like as :

<script>

let myname1=new String(“Hello World”);

document.write(myname1);

</script>

  • 2.Multi line strings can be defined without closing the quotes at the end of the line.

let  text = ‘the sample

                  program’;

Methods used along with String object in Java Script:

Basic string operations are string concatenation, substring extraction, string length and so on.

                             String methods used are:

  1.Con cat()                      Concatenates two strings and redirects the output to the specified letiable.

  2.charAt(index)              The character specified at the index position given  inside the parenthesis will be displayed

 3. sub string(start,end)    Returns the part of the string

 4. split()                            Divides the string

  5.to Lower Case()             Converts the Upper case letters into Lowercase letters

6.. to Upper Case()             Converts the Lower case letters to Upper case letters

7. replace()                       Replaces a string with another string

8.  trim()                            Removes blank spaces  in the string

9.. index Of()                      Returns the last index position of the string

10. Includes()                    Search whether a string is present or not. It is case sensitive.

11.repeat()                       Repeats the string as many number as specified

12.replace()                      Replaces the first string with the second one.

13.slice()                           Slices the string at the specified position.

14.split()                           Splits the string into sub strings at the specified character occurrence.

  • 15.substr()                        Same as sub string() method

Sample Programs of Java Script:

        1)    <script>

         let string 1 = “my java script”;

let string 2 = “Example”;

let string3 = string1.concat(string2);

document.write(string3);

</script>

The result of this program is :–            my java script Example

2)   <script>

let s = “my script”;

document.write(s.charAt(2));

</script> 

The result of this program is :–   y

3)  <script>

     let s1 = “my javascript example”;

      let n = s1.indexOf(“example”);

     document.write(n);

   </script>

The program output will be         16 

The  starting position of the string “example” 

4) <script>

     let s1 = “My first Javascript”;

     let s2 = s1.toLowerCase();

     document.write(s2);

     </script>

The output will be     my first javascript    

5)  <script>

     let s1 = “My first Javascript”;

     let s2 = s1.toUpperCase();

     document.write(s2);

     </script>

The output will be     MY FIRST JAVASCRIPT    

6) <script>

     let s1 = “My first Javascript”;

     let s2 = s1.trim();

     document.write(s2);

     </script>

The output will be     MyfirstJavascript

7)  <script> 

     let s1 = “my javascipt program”;

    document.write(s1.includes(“program”));

    document.write(s1.includes(“PrograM”));

   </script>

Output will be :

                    true

                    false

8)  <script>

     let s1 = “example”;

     document.write(s1.repeat(3));

   </script>

The output will be as follows:

      example  example example

        9)   <script>

              let s1 = “my example”

             document.writes1.replace(“my”, “MY”);

            </script>

       The output will be MY example

        10) <script>

               let s1 = ‘first program’;

               document.write(s1.slice(2,5));

            </script>

              The output will be    rst

      11)   <script>

                 let s1 = ‘my first javascript’;

                 document.write(s1.trim());

               </script>

           The output will be     myfirstjavascript

Conclusion: 

                    Javascript is now used for many applications.  There are many string methods and properties yet to be explained. Most popularly

 used ones are included in this article.

Java Script Strings Step By Step Guide

Leave a Reply

Your email address will not be published.

Scroll to top