Monday 26 February 2018

Learn How to use variables in the JavaScript code

Here We need to Properly declare a variable to use it in the script for storing data. Now we create a Web page named AssignValToVariable.html which shows how to assign values to the variables in the script.

Shows the code for the AssignValToVariable.html file.

<!doctype html>
<html>
<head>
<title>Declaring a variable</title>
</head>
<body>
<h1>Using a variable in the script</h1>
<SCRIPT type="text/javascript">
var countBooks=100;
document.write("Number of Books="+countBooks);
</SCRIPT>
</body>
</html>

Here We declared a variable named countBooks and assigned the value 100 to it.
The statement document.write ("Number of Books"+countBooks) prints the value of the countBooks variable. The + operator is used to append the two strings the text Number of Books= and the value of the countBooks variable.

Shows the output of the AssignValToVariable.html file.









No comments:

Post a Comment