Using external JavaScript file and understanding variable.

Posted in by Nazrul Islam . Filed in JavaScript


In our previous lesson, we learn how to write JavaScript inside HTML markup. But in practical field some time we need to write large JavaScript code, which could easily messed up with HTML markup. That’s why we have to use external JavaScript file. Another advantage is, we could use external JavaScript file in multiple page and could be reused in future project.

External  JavaScript file must have .js extension . To use an external JavaScript file, first we need to link up it with HTML markup like <script type=”text/javascript” src=”MyJavaScript.js”></script>. After linking external JavaScript file like this, we need to manually create this file  MyJavaScript.js. File naming doesn’t require any special rule, but make sure that, file name is same on you HTML file where you linked it. This is very much similar to linking external CSS file. I hope you already figured it out.

Look at my file naming in our code. I named it MyJavaScript.js, which actually contains three separate words by meaning. Every word start with a upper case latter but no spacing. This naming method known as Camel Case. java Script use Camel Case for its own function. In future you will see many code like getElementById(‘myText’); which actually use Camel Case naming method. I recommend you to use Camel Case naming method for JavaScript.

OK now its time get familiar with variable, what is variable actually ? The answer is” variable is sort of a data container”. We can store data on a variable and reuse or manipulate them to achieve targeted output. Other way you can say it as an identifier of data.

To declare a variable, we have to use var keyword. It’s a reserved keyword. We will discuss later about reserved keyword. Declaring variable is very simple,  just type var and then the variable name. Format is var tom;  here we declare a variable, but we didn’t specify any data type. It may contain many type of data like  integer, floating point or string. Actually we don’t need to specify  any data type in JavaScript unlike C or C++. JavaScript automatically set data type, this automatic data type casting is known as weakly-typed feature.

We also didn’t assign any value to tom, it will remain undefined until value assigning . Without assigning a value, A variable also give output undefined. You can assign a value like  var tom; tom = 20; Here we declare a variable then assign it’s value to 20.

We can also write this code directly as var tom = 20; . To declare multiple variable in a single statement just use a comma to separate them like var tom = 20, jerry = 50; .   To declare a string variable we have to use a single quote or double quote. As an example var spiderman = “ Spiderman is a super hero ”;  or var spiderman = ‘ Spiderman is a super hero ’; both format is right. That’s all for today. Happy coding!


Tags:


1 comments
helaluddin
helaluddin

Thanks, I hope you will update next tutorial soon.