Write simple JavaScript with HTML for arithmetic expression evaluation and message printing

Rahul Tamkhane
3 min readDec 23, 2020

--

LABORATORY OBJECTIVE:
1. To be able to write simple JavaScript programs.
2. To be able to use input and output statements.
3. 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 to 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 a your webpage HTML file.

<script language = “javascript” type=“text/javascript”>

</script>

Hello World program in JavaScript

Program

<html>
<head>
<title>First JavaScript Program</title>
</head>
<body>
<script type=”text/javascript”>
document.write(“Hello World!”);
</script>
</body>
</html>

Output

Hello World Program
Output: Hello World Program

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>
Message Printing Using Alert
Output: Message Printing Using Alert

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>
Arithmetic Operation — Addition, Subtraction, Multiplication and Division
Output: Arithmetic Operation - Addition, Subtraction, Multiplication and Division

Thank you for your support

You can try out my apps available on Google Play Store

  1. Coding Hub App — https://play.google.com/store/apps/details?id=com.rahultamkhane.codinghub
  2. PocketJS App — https://play.google.com/store/apps/details?id=com.rahultamkhane.pocketjs
  3. JavaProgramming App — https://play.google.com/store/apps/details?id=com.rahultamkhane.javaprogramming

--

--

No responses yet