Examples of JavaScript


Prev Tutorial Next Tutorial

Example of JavaScript

Here i will show you how to write you first javascript code, you only need to write your javascript code inside <script> ..... </script> tag using any editor like notepad or edit plus. Save below code with .html or .htm extension. No need to compile your javascript code just open your code in any web browser.

Find sum of two number using JavaScript

Example

<!doctype html>
<html>
<head>
<script>
function add(){
var a,b,c;
a=Number(document.getElementById("first").value);
b=Number(document.getElementById("second").value);
c= a + b;
document.getElementById("answer").value= c;
}
</script>
</head>
<body>
<input id="first">
<input id="second">
<button onclick="add()">Add</button>
<input id="answer">
</body>
</html>

Result

Enter First Number:
Enter Second Number:

Code Explanation

  • no=Number(document.getElementById("first").value);
    This code is used for receive first input value form input field which have id first.
  • no=Number(document.getElementById("second").value);
    This code is used for receive first input value form input field which have id second.
  • document.getElementById("answer").value= fact;
    This code is used for receive calculated value of factorial and display in input field which have id answer
  • <button onclick="add()">Add</button>
    This code is used for call add function when button clicked.

Prev Tutorial Next Tutorial

Google Advertisements
Download Projects

Google Adsense Advertisements

Buy 3 Year Old Domain, Tutorialday.com, SeosmoTools.com @$200

Buy This Ad Space @$50 per Month, Ad Size 300X250 Contact on: hitesh.xc@gmail.com WhatsApp no. 8882538826

Yahoo Advertisements