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.
Output :
this is external js
Usage of Variables in JavaScript :-
The keyword we use here is var.
Syntax : var nameofthevariable;
Example
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 :
Example :
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 :
Note:- there was no need to use var to create multiple variables you can also use the above approach.
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
JavaScript Introduction Part 2 : Checkout : Part 2
Konakalla Premsai I love to make things simple :) |
||||