ddsingh@infomatics.info +91 9760014754

JAVA SCRIPT COMMENTS

Comments can be added to explain the JavaScript, or to make the code more readable.

1)Single line comments start with //

2)JavaScript Multi-Line Comments  /*……….*/

3)HTML Comment     <!—Comment - ->

We Can use Java Script by Two Ways

1)Internal JavaScript

2)External JavaScript

 

In the case of internal java script we embed the java script code within html.


Using an External JavaScript:-

JavaScript can also be placed in external files.

External JavaScript files often contain code to be used on several different web pages.

 

External JavaScript files have the file extension .js.

 

Note: External script cannot contain the 

 

<script>   </script> tags!

To use an external script, point to the .js file in the "src" attribute of the <script> tag.

<html>

<body>

<script type="text/javascript" src="xyz.js">

</script>

</body>

</html>

Ex:-ExternalScriptDemo

function get()
{
	document.write("hello world");
}
<html>
<head>
   <script type="text/javascript" src="A.js">
  </script>
<body>
  <input type="button" onclick="get()" value="click" />
</body>
</head>
</html>

Using a Semicolon:-

every statement is followed by; (optional)

Note:-

If We are having one statement in  single line within java script file In that case semicolon is optional. But if we are having more than one statement in single line in that case semicolon is mandatory.

Declaring (Creating) Variables

 

Creating variables in JavaScript is most often referred to as "declaring" variables.

 

You declare JavaScript variables with the var keyword:

ex:-

var x;

var carname;

 

After the declaration shown above, the variables are empty (they have no values yet).

However, you can also assign values to the variables when you declare them:

var x=5;

var carname="Volvo";

After the execution of the statements above, the variable x will hold the value 5, and carname will hold the value Volvo.

Note: When you assign a text value to a variable, use quotes around the value.

Ex:-prg6,7.html

<html>
<head>
<script type="text/javascript">
		var a=2;
		document.write("value="+a);
</script>
</head>
</html>
<html>
<head>
<script type="text/javascript">
		var a=2;
		var b=4;

		var linebreak="<br/>";
		var sum,mul;
		sum=a+b;
		document.write("sum="+sum);
		document.write(linebreak);
		mul=a*b;
		document.write("mul="+mul);
</script>
</head>
</html>

 


Prev Next