Basic Rules of JavaScript Syntax

Basically JavaScript syntax is used to write JavaScript properly. In definition, it is a set of rules that define how JavaScript program are constructed or correctly structured JavaScript program.

 

Now I am sharing some rule of JavaScript syntax :

Use the script tag to tell the browser you are using JavaScript:
==============================================

JavaScript can be written in html that are placed within the <script>… </script> tags in a web page. The <script> tags contain the full JavaScript and its place anywhere within you web pages. But it is normally preferred that you should keep it within the <head> section. The <script> tag tell the browser program that all the text between these tags as a script.

 

Two important attributes are available in the <script>  tag –

  • Language=”javascript” —language attribute specifies that what scripting language you are using and the value normally be javascript.
  • Type=”text/javascript” —the type attribute indicate the scripting language in use and its value should be set to “text/javascript”.

Here is an example:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
	<meta charset="UTF-8">
	<title></title>
</head>
<body>
	<script language="javascript" type="text/javascript">
		// JavaScript code goes hear.
	</script>
</body>
</html>

 

Using semicolons in JavaScript are optional:
=================================

JavaScript does not require that we use semicolons to signify the end of each statement but it is good to use semicolon at the end of the statement. But only is required  when we want to write two statement within a single line. See example below:

document.write(“This is text”); document.write(“This is another text”);

NOTE: (document.write is used to write text in a html document by JavaScript)

 

JavaScript Comments syntax:
=======================

Single line comment — use double forward slashes and give your comments

// comments goes hear

Multi line comment — that is start forward slash with asterisk (*) sign then given your comments and end multi line comment by using asterisk (*) sign with backslash.

/*

comments goes hear

comments goes hear

comments goes hear

comments goes hear

*

 

JavaScript is highly case sensitive:
==========================

JavaScript is a case-sensitive language. All JavaScript identifiers are case sensitive.

Like:

 

alert(“this is alert”); // Right statement

Alert(“this is alert”); // wrong statement

 

 

JavaScript ignore line break and white-space:
==================================

JavaScript is not count spaces, tabs, and newlines when we write JavaScript code. So we can freely use spaces, tabs, and newlines in our program to write our programs neat and clean, in the result the code looks nice and make it easy to read.

I am showing a example below to write code in proper way:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
	<meta charset="UTF-8">
	<title></title>
</head>
<body>
	<script type="text/javascript">
		document.write("Wow javascript is coool!!!");
	</script>
</body>
</html>