Syntax and Placement of JavaScript

  • The syntax of JavaScript are the set of rules that define correctly structure JavaScript program.
  • The examples below make uses of the log function of the console object present in most browsers for standard text output.
  • The JavaScript standard library lack an official standard text output function . Given that JavaScript is mainly uses for client-side scripting within modern Web browsers, and that almost Web browsers provide the alert function, alert can also be used, but is not commonly used.
JavaScript syntax refers to the set of rules that is determines how JavaScript programs are constructed:

Basic Syntax

<script>  

document.write("Basic Print method in JavaScript");  

</script>  

Case sensitivity

JavaScript is case sensitive . It is commonly to start the name of a constructor with a capitalise letter, and the name of a function or variable with a lower-case letter.

let a=6;

console.log(a); // 6

console.log(A); // throws a ReferenceError: A is not defined

Variables: A JavaScript variable is the simple name of storage location where data to be stored . There are two types of variables in JavaScript which are listed below:
  • Local variables: Declare a variable inside of block or function.
  • Global variables: Declare a variable outside function or with window object.

let globalVar = “This is a global variable”;

function fun(){

let localVar = “This is a local variable” ;  

let localVar = “This is a local variable”; 

console.log(globalVar);

console.log(localVar);

}

fun();

After code run

JavaScript Placement

In Javascript, SCRIPT tags can be inserted into three places: in between the two BODY tags, in between the two HEAD tags, and as a link to an external file, also in the HEAD section.Some advocate putting the SCRIPT tags in the BODY section, right at the end. Like this:

! DOCTYPE HTML

<html>  

<head>  

<Title>  A first script </Title>

  </head> 

 <Body> 

  <script language = ” JavaScript > 

  </script> 

 </Body> 

 </html>  

On Page JavaScript

  • You can write JavaScript on HTML pages anywhere between and tags.
  • All on page JavaScript code must be inside </script>  tag
  • In script below, document.write is used to calculate the result of 5+6.
  • A semicolon ; represents of end of statement in JavaScript.

External JavaScript

  1. You can write all your JavaScript code in an external file and then link that file to any of your HTML pages.
  2. External JavaScript files use a .js extension, for example, myfile.js or external-javascript.js
  3. To use an external JavaScript file, use the <script>  tag with src attribute, followed by the path to your external javascript file.
  4. You can include resources in the head tag but now a days it is recommended to place JavaScript just before closing Body Tag.
  5. You can include script at the end of document.

<html>  

<head>  

<script> 

  </head>  

<body> 

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

</body>

 <html>  

Leave a Reply

Your email address will not be published. Required fields are marked *