Write simple JavaScript with HTML for arithmetic expression evaluation and message printing
Laboratory Objective:
- To be able to write simple JavaScript programs.
- To be able to use input and output statements.
- To be able to use arithmetic operators.
What is JavaScript?
- JavaScript is an interpreted, client-side, event-based, object oriented scripting language that you can use to add dynamic interactivity to your web pages.
- It was designed to add interactivity to HTML pages
- It is an interpreted language (means that scripts execute without preliminary compilation)
- JavaScript scripts are written in plain text, like HTML, XML, Java, PHP and just about any other modern computer code.
Uses of JavaScript
JavaScript can be used to achieve any of the following:
- Create special effects with images that give the impression of a button.
- Validate information that users enter into your web forms.
- Open pages in new windows, and customise the appearance of those new windows.
- Detect the capabilities of the user’s browser and alter your page’s content appropriately.
- Create custom pages “on the fly” without the need for a server-side language like PHP.
Adding JavaScript to Webpage Using <script> tag
To use JavaScript in your program just insert <script> …. </script> tag either in <HEAD> or <BODY> section of your webpage HTML file.
<script language = “javascript” type=“text/javascript”>
</script>
Hello World program in JavaScript
<html>
<head>
<title>First JavaScript Program</title>
</head>
<body>
<script type=”text/javascript”>
document.write(“Hello World!”);
</script>
</body>
</html>

SAMPLE PROGRAMS
1) Develop a JavaScript program to display ‘Hello World!’ message in alert box.
<html>
<head>
<title>Message Printing</title>
</head>
<body>
<script>
alert("Hello, world!");
</script>
</body>
</html>

2) Develop a JavaScript program to perform following arithmetic operations: addition, subtraction, multiplication and division.
<html>
<head>
<title>Arithmetic Operations</title>
</head>
<body>
<script type="text/javascript">
var a = 12;
var b = 34;
var result;
document.write("Value of a = " + a + " and b = "+ b);
result = a + b;
document.write("<br>Addition of a & b = " + result );
result = a - b;
document.write("<br>Subtraction of a & b = " + result );
result = a * b;
document.write("<br>Multiplication of a & b = " + result );
result = a / b;
document.write("<br>Division of a & b = " + result );
</script>
</body>
</html>
