0% found this document useful (0 votes)
28 views

Manual of Javascript

Uploaded by

suchita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Manual of Javascript

Uploaded by

suchita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Example 1: JavaScript Program to Print Hello World

<html>

<body>

<script type="text/javascript">

alert("Hello World");

</script>

</body>

</html>

Output

Example 2: JavaScript Program to Find the Factorial of a Number


<!DOCTYPE html>
<html>
<head>
</head>
<body style = "text-align: center; font-size: 20px;">
<h1> Welcome to the javaScript world!! </h1>
Enter a particular number: <input id = "num">
<br><br>
<button onclick = "fact()"> Please type any Factorial number </button>
<p id = "res"></p>
<script>
function fact(){
var i, num, f;
f = 1;
num = document.getElementById("num").value;
for(i = 1; i <= num; i++)
{
f = f * i;
}
i = i - 1;
document.getElementById("res").innerHTML = "The factorial of the number " + i + " is: " +
f;
}
</script>
</body>
</html>
Output

Example 3 JavaScript Program to Format the Date With Expected Output


mm-dd-yyyy, mm/dd/yyyy or dd-mm-yyyy, dd/mm/yyyyvar today = new Date();

var dd = today.getDate();

var mm = today.getMonth()+1;

var yyyy = today.getFullYear();

if(dd<10)

dd='0'+dd;

if(mm<10)

mm='0'+mm;

}
today = mm+'-'+dd+'-'+yyyy;

console.log(today);

today = mm+'/'+dd+'/'+yyyy;

console.log(today);

today = dd+'-'+mm+'-'+yyyy;

console.log(today);

today = dd+'/'+mm+'/'+yyyy;

console.log(today);

Output
11-10-2021

11/10/2021

10-11-2021

10/11/2021

Example 4: JS Form Program Example


<!DOCTYPE html>

<html>

<head>

<script>

function validateForm() {

let x = document.forms["myForm"]["fname"].value;

if (x == "") {

alert("Please enter your Name");

return false;

</script>
</head>

<body>

<h2>JavaScript Test Validation</h2>

<form name="myForm" action="/action_page.php" onsubmit="return validateForm()"


method="post">

Enter Name: <input type="text" name="fname">

<input type="submit" value="Submit">

</form>

</body>

</html>

Output

Example 5: POPUP Message Program Using Event


<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Confirm Box</h2>
<button onclick="myFunction()">Please Try it</button>
<p id="Test Confirm Box"></p>
<script>
function myFunction() {
var txt;
if (confirm("Please Press a button!")) {
txt = "You pressed Button!";
} else {
txt = "You pressed Cancel Button!";
}
document.getElementById("Test Confirm Box").innerHTML = txt;
}
</script>
</body>
</html>
Output

Example 6: Display Alert for Prompt Message Program


<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Prompt Example</h2>
<button onclick="myFunction()">Please Try for Prompt message</button>
<p id="Prompt Example"></p>
<script>
function myFunction() {
let text;
let user = prompt("Please enter your name:", "Your First Name");
if (user == null || user == "") {
text = "User cancelled the prompt.";
} else {
text = "Hello " + person + "! How are you?";
}
document.getElementById("Prompt Example").innerHTML = text;
}
</script>
</body>
</html>
Output
Example 7: Line-Breaks Pop-Up Message

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<p>Line-breaks Example in a popup box.</p>
<button onclick="alert('Hello\nHow are you?')">Please Try for line-breaks
Example</button>
</body>
</html>

Output

Example 8: JS Screen Program Using Javascript


<!DOCTYPE html>
<html>
<body>
<p id="ScreenColorDepth"></p>
<script>
document.getElementById("ScreenColorDepth").innerHTML =
"Screen color depth is " + screen.colorDepth;
</script>
</body>
</html>
Output
Example 9: JavaScript Timer
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Timing Sample</h2>
<p>Click on "Try it". Wait 5 seconds, and the page will alert "Hello How are you!!".</p>
<button onclick="setTimeout(myFunction, 5000);">Try it</button>
<script>
function myFunction() {
alert('Hello How are you!!');
}
</script>
</body>
</html>
Output
Practical Lab Programs in Javascript
1. Write a JavaScript program to swap the value of two variables without using a third
variable. Take the value of both variables from the user.
2. Write a JavaScript program to calculate the area of a square
3. Write a Javascript program to convert Kilometers to centimetrs
4. Develop a JavaScript program to perform following arithmetic operations: addition,
subtraction, multiplication and division.

Solutions

1)Write a JavaScript program to swap the value of two variables without using a third
variable. Take the value of both variables from the user.
<html>
<head>
<title>Javascript</title></head>
<body>
<script type="text/javascript">
var x=parseInt(prompt("Enter first number"));
var y=parseInt(prompt("Enter second number"));
x = x+y ;
y = x -y ;
x = x -y ;
document.write("x =");
document.write(x);
document.write("<br>");
document.write("y =");
document.write(y);
</script>
</body>
</html>

2) Write a Javascript program to calculate the area of a square


<html>
<head>
<title>Area of square</title></head>
<body>
<script type="text/javascript">
var s=parseInt(prompt("enter the side of square"));
var area;
area=s*s;
document.write("Area of square=");
document.write(area);
</script>
</body></html>
3)Write a Javascript program to convert Kilometers to centimetrs
<html>
<head>
<title>javascript</title></head>
<body>
<script type="text/javascript">
var km=parseInt(prompt("enter the value in Kilometer"));
var cm;
cm=100000*km;
document.write("kilometre to centimetre=");
document.write(cm);
</script>
</body>
</html>

4 )Develop a JavaScript program to perform following arithmetic operations: addition,


subtraction, multiplication and division.
<html>
<head>
<title>Arithmetic operators in Javascript</title></head>
<body>
<script type="text/javascript">
var a=parseInt(prompt("enter first number"));
var b=parseInt(prompt("enter second number"));
var sum;
var difference;
var product;
var quotient;
sum=a+b;
difference=a-b;
product=a*b;
Quotient=a/b;
document.write("Sum= ");
document.write(sum);
document.write("<br>");
document.write("Difference =");
document.write(difference);
document.write("<br>");
document.write("Product=");
document.write(product);
document.write("<br>");
document.write("Quotient=");
document.write(quotient);
</script>
</body>
</html>

You might also like