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.


       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.


     let myname1 = new String (“abcd”);

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


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



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

let  text = ‘the sample


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 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);



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

2)   <script>

let s = “my script”;



The result of this program is :–   y

3)  <script>

     let s1 = “my javascript example”;

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



The program output will be         16 

The  starting position of the string “example” 

4) <script>

     let s1 = “My first Javascript”;

     let s2 = s1.toLowerCase();



The output will be     my first javascript    

5)  <script>

     let s1 = “My first Javascript”;

     let s2 = s1.toUpperCase();



The output will be     MY FIRST JAVASCRIPT    

6) <script>

     let s1 = “My first Javascript”;

     let s2 = s1.trim();



The output will be     MyfirstJavascript

7)  <script> 

     let s1 = “my javascipt program”;




Output will be :



8)  <script>

     let s1 = “example”;



The output will be as follows:

      example  example example

        9)   <script>

              let s1 = “my example”

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


       The output will be MY example

        10) <script>

               let s1 = ‘first program’;



              The output will be    rst

      11)   <script>

                 let s1 = ‘my first javascript’;



           The output will be     myfirstjavascript


                    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. Required fields are marked *

Scroll to top