896+ Capstone Project is Available: Whatsapp: +91-7011258995, Email: sharecodepoint@gmail.com

JavaScript Tutorial : JavaScript in External File with examples - JavaScript Part 3

As we begin to work extensively in JavaScript,we will be likely to find that there are cases that where we are using identical JavaScript code on multiple pages. The Script tag allow us to store the JavaScript in a external file and the include to HTML files.

Example 1 :
<html> 
 <title>linking external JavaScript file</title> 
 <head> 
  <script src="1.js" type="text/javascript"></script>
 </head> 
 <body> 
 </body>
</html>
  • Save this code as abc.html 
  • External JavaScript file   document.write("this is external js"); 
  • save this code with the extension of .js like 1.js

Output :
this is external js

Usage of Variables in JavaScript :-
The keyword we use here is var.

Syntax : var nameofthevariable;

Example
<html>
 <head>
 </head>
 <body> 
 <script>
   var a='example of variable'; 
   var b='10';// 
   var c=15; //declaration of an integer. 
    
    document.write(a); 
    document.write(b); 
    document.write(c); 
  </script> 
 </body> 
</html>
Output :
example of variable 1015

Now the data we put inside the single quotes are treated as string data. To declare an integer we must avoid using single quotes. That will be clearly explained in the following example.

Example :
<html>
 <head>
 </head>
  <body> 
    <script > 
        var b='10'; 
        var c=15; 
        var d=c+b; 
        
        document.write(d); 
    </script> 
   </body>
</html>
Output : 1015

Example :
<html>
  <head> 
  </head>
     <body>
        <script> 
           var b = 10; 
           var c = 15; 
           var d = c+b; 
 
           document.write(d); 
         </script> 
      </body> 
</html>
Output : 25

Explanation:- As we observed in the first example the variable b’s data was given inside the singe  quotes so is was stored as an string type. Now in the second example we declared all the variable as an integer so there Ari thematic operation was done.

Note: var a=10;var b=20;var c=’a+b’;

document.write(c);//output=a+b

because we have given the data inside an singlequotes.

Example :
<html> 
  <head>
  </head>
    <body> 
      <script > 
         var b=10, c=15,d=c+b; 
          document.write(d); 
      </script> 
   </body>
</html>
Output : 25
Note:- there was no need to use var to create multiple variables you can also use the above approach.


JavaScript Introduction Part 1 : Checkout : Part 1

JavaScript Introduction Part 2 : Checkout : Part 2
About Developer
Konakalla Premsai
I love to make things simple :)

Sharecodepoint

Sharecodepoint is the junction where every essential thing is shared for college students in the well-defined packets of codes. We are focused on providing you the best material package like Question papers, MCQ'S, and one NIGHT STUDY MATERIAL. facebook twitter youtube instagram

Post a Comment

Previous Post Next Post

Contact Form