Where to JavaScript Code Fits

JavaScript file can be placed in an HTML page with any of  the following methods.

1. By placing the Script code in the Script Tags.

Any script code can be placed in side the <script>---</script> tags at place inside the webpage.


For examples

<script>
function sayHi() {
alert("Hello world!");
 }
 function sayBye() {
alert("Buh-bye!");
}
 </script>

or

<script language=”JavaScript” type=”text/javascript”>
function sayHi() {
alert("Hello world!");
 }
 function sayBye() {
alert("Buh-bye!");
}
 </script>

Above both scripts are equivalent. Second script tag defines some more attributes and these are optional.
2. Script code can be written in seperate file and saved with .js exetension and then reference to this file. For example legal names for JavaScript file can be.

scripts.js
coreJS.js
main-code.js
main_code.js
main.code.js


Then we can refrence the this file in our Web page any where by using  the following refrence tags

<script language=”JavaScript” type=”text/javascript” src=”filename.js”></script>


We can palce the JavaScript Code in our page usually at the following four places.

1. In the body of the page—In this case, the script’s output is displayed as part of the HTML document when the browser loads the page.
2 . In the header of the page between the <head> tags—Scripts in the header don’t immediately affect the HTML document, but can be referred to by other scripts. The header is often used for function, groups of JavaScript statements that can be used as a single unit.
3. Within an HTML tag, such as <body> or <form>—This is called an event handler and enables the script to work with HTML elements. When using JavaScript in event handlers, you don’t need to use the <script> tag.
4.  In a separate file entirely—JavaScript supports the use of files with the .js
extension containing scripts; these can be included by specifying a file in the
<script> tag.

As from above paragraph we can place the JavaScript code anywhere in our webpage but it is good and usual practice to place the JavaScript code at the end of the webpage. Which decrease the loading time for the webpage. But in now modern days of 4G Internet this is not the matter.

Arithmetic operation is performed in the order as described by algebra and we can only change the priority of order by using parenthesis.
For example.

var resultOfComputation = (2 * 4) * 4 + 2;

By placing the first multiplication operation inside parentheses, you've told JavaScript to
do that operation first. But then what? The order could be..

1. Multiply 2 by 4.
2. Multiply that product by 4.
3. Add 2 to it. ...giving resultOfComputation a value of 34.
Or the order could be...
1. Multiply 2 by 4.
2. Multiply that product by the sum of 4 and 2.
...giving resultOfComputation a value of 48.
The solution is more parentheses.
If you want the second multiplication to be done before the 2 is added, write this...
resultOfComputation = ((2 * 4) * 4) + 2;
But if you want the product of 2 times 4 to be multiplied by the number you get when you
total 4 and 2, write this...
resultOfComputation = (2 * 4) * (4 + 2);