No icon

JAVASCRIPT TEMPLATE LITERALS

JAVASCRIPT TEMPLATE LITERALS

JAVASCRIPT TEMPLATE LITERALS

TEMPLATE LITERALS  : It is usefull whenever you want to print any string in that string if any variable is present and if you want to print any data with our required spaces in output and below examples will clearly shows example code along with their outputs clearly.

CONCATENATION EXPLANATION 

If you want place your variable value in/along with output string , in JavaScript to do this you need to use concatenation concept

Example : 

<html> 
      <head>
            SCRIPT  

            CASE -1 [In this Concatenation doesn,t happened]
                  var name="Ahamad";
                  Document.write('Hello name);         => OUTPUT [Hello name]
            
            CASE -2  [ If you want place your variable value in output string , in JavaScript to do this you need to use
concatenation concept ]
            Below Example Shows
concatenation
            
                  var name="Ahamad";
                  Document.write('Hello '+name);        

                  => OUTPUT [Hello Ahamad]

            CASE -3  [ For big messages these type of concatination may leads to heavier the things, example given below]
            
                  var name="Ahamad";
                  Document.write('Hello '+name+', Good Morning' ); 
                  
                  => OUTPUT [Hello Ahamad , Good Morning]

            /SCRIPT

                        
      </head>

             <body>

             </body>

</html>


To avoid above complexity in concatenation TEMPLATE LITERALS Will helps you , Below Example will clearly explains you along with TEMPLATE LITERALS stucture.

EXAMPLE : With Template Literals Everytime PLUS [adding] using maynot require ,While we are creating string we may use single quote or double quote but if you use TEMPLATE LITERALS you will replace single/double quotes with backticks.

But in TEMPLATE LITERALS we are using backtick [`] instead of single/double quote while declaring string.

EXAMPLE :
<html> 
      <head>

            SCRIPT
      
            var name="Ahamad";
      
            Document.write(`Hello ${name}, Good Morning`);
            
            /SCRIPT
      
      </head>
      
      <body>
      
      </body>
</html>

 

TEMPLATE LITERALS USE 

EXAMPLE :

<html> 
      <head>

            SCRIPT
      
            console.log ("Hello Ahamad \n Good Morning"); 
            
            OUTPUT : Hello Ahamad
                       Good Morning
                     
            console.log ("Hello Ahamad 
            Good Morning"); 

            
Note  : If you want to print your Data in two lines without \n it through a error
Error :    Uncaught SyntaxError: Invalid or unexpected token
        

Above Problem of printing of our required spaces in output will resolved with usage of Template Literals. with using of backticks.

Below Example will Let you know how to print your Data with how much space you give in your code that much only present in the output                     
                     
            console.log (`Hello Ahamad 
            Good Morning`); 

            
            OUTPUT : Hello Ahamad 
                                  Good Morning
                     
            /SCRIPT
      
      </head>
      
      <body>
      
      </body>
</html>

TEMPLATE LITERALS is usefull whenever you want to print any string in that string if any variable is present and above examples will clearly shows codealong with their outputs clearly, if you have any doughts regarding TEMPLATE LITERALS contact us.

Comment As:

Comment (0)