Javascript Programs
Javascript Programs
<html>
<body>
<script>
document.write(“Hello Javascript”);
</script>
</body>
</html>
<html>
<body>
<script>
Var x=10;
Var y=20;
Var z=x+y;
document.write(z);
</script>
</body>
</html>
Q3. Write a Javascript Program to show date and time on web page?
<html>
<body>
<script>
document.write(d);
</script>
</body>
</html>
<html>
<head>
<script>
function myFunction()
{
alert("HELLO!");
}
</script>
</head>
<body>
<input type="button" onclick="myFunction()" value="show me the
alert box"/>
</body>
</html>
Or
<html>
<head>
<script>
alert("HELLO!");
</script>
</head>
<body>
</body>
</html>
II. CONFIRM
<html>
<head>
<p>Click the button to display a confirm box.</p>
<button onclick="myFunction()">try it</button>
<p id="demo"></p>
<script>
function myFunction()
{var x;
var r=confirm("press a button");
if(r==true)
{
x="you pressed OK";
}
else
{
x="you pressed cancel";
}
document.getElementById("demo").innerHTML=x;
}
</script>
</body>
</html>
OR
<html>
<head>
<script>
confirm("HELLO!");
</script>
</head>
<body>
</body>
</html>
III. Prompt
<html>
<body>
<p id="demo"></p>
<script>
function myFunction()
{
var x;
var person;
if(person!=null)
document.getElementById("demo").innerHTML=x;
</script>
</body>
</html>
OR
<html>
<head>
<script>
</script>
</head>
<body>
</body> </htmL>
Q5. Write a Javascript Program to show how to use function or call a
function?
<html>
<head>
<script>
function abc()
{
document.write("Welcome");
}
</script>
</head>
<body>
<input type="button" onclick="abc()" value="Calling a function"/>
</body>
</html>
<html>
<body>
<script>
function myFunction(name,object)
alert(name+"revolves"+object);
</script>
</body></html>
Q7. Write a Javascript Program to show Function that returns a
value.?
<html>
<head>
<script>
function myFunction()
</script>
</head>
<body>
<script>
document.write(myFunction());
</script>
</body>
</html>
<html>
<body>
<p id="demo"></p>
<script>
function myFunction(x,y)
{
return x+y;
document.get ElementById("demo").innerHTML=myFunction(5,8);
</script>
</body>
</html>
String method Programs
Q9. Write a Javascript Program to find the length of the array?
<html>
<body>
<script>
document.write(txt.length);
</script>
</p>
</body>
</html>
<html>
<body>
occurs.</strong></p>
<script>
function myFunction()
document.getElementById("demo").innerHTML=n;
</script>
</body>
</html>
<html>
<body>
<script>
document.write(str.match("policy")+"<br>");
document.write(str.match("Police")+"<br>");
document.write(str.match("pollicy")+"<br>");
document.write(str.match("policy")+"<br>");
</script>
</body>
</html>
Q12. Write a Javascript Program to replace characters in a string
using replace() ?
<html>
<body>
<script>
function myFunction()
var str=document.getElementById("demo").innerHTML;
document.getElementById("demo").innerHTML=n;
</script>
</body>
</html>
Math method Programs
Q13. Write a Javascript Program to round off any number using
round() ?
<html>
<body>
<p id="demo">click the button to round the no.to its nearest integer.</p>
<script>
function myFunction()
document.getElementById("demo").innerHTML=Math.round(8.7896);
</script>
</body>
</html>
<html>
<body>
<script>
function myFunction()
document.getElementById("demo").innerHTML=Math.random();
}
</script>
</body>
</html>
<html>
<body>
<p id="demo">Click the button to return the highest no. between 5 and
10.</p>
<script>
function myFunction()
document.getElementById("demo").innerHTML=Math.max(5,10);
</script>
</body>
</html>
Q16. Write a Javascript Program to return the number with the lowest
value of two specified number using min() ?
<html>
<body>
<p id="demo">Click the button to return the lowest no. between 77 and 9.
</p>
<button onclick="myFunction()">try it</button>
<script>
function myFunction()
document.getElementById("demo").innerHTML=Math.min(77,9);
</script>
</body>
</html>
<html>
<body>
<script>
var i;
fruits[0]="apple";
fruits[1]="banana";
fruits[2]="orange";
for(i=0;i<fruits.length;i++)
{
document.write(fruits[i]+"<br>");
</script>
</body>
</html>
<html>
<body>
<script>
function myFunction()
var fruits=["Apple","Orange"];
var vegetables=["Cucumber","Carrot","Raddish"];
var grains=["Wheat","Maize"];
var mix=fruits.concat(vegetables,grains);
var x=document.getElementById("demo");
x.innerHTML=mix;
</script>
</body>
</html>
Q19. Write a Javascript Program to remove the last element from the
array by using pop() ?
<html>
<body>
<script>
var name=["John","Mary","Oliver","Alice"];
function myFunction()
name.pop();
var x=document.getElementById("demo");
x.innerHTML=name;
</script>
</body>
</html>
<html>
<body>
<script>
var name=["Alice","Henry","John","Leo"];
function myFunction()
name.push("Aayush");
var x=document.getElementById("demo");
x.innerHTML=name;
</script>
</body>
</html>
<html>
<body>
<p id="demo">Click the button to reverse the order of the element in the
array.</p>
<script>
var alphabet=["z","k","j","h","e"];
function myFunction()
alphabet.reverse();
var x=document.getElementById("demo");
x.innerHTML=alphabet;
}
</script>
</body>
</html>
<html>
<body>
<script>
function myFunction()
var fruits=["Banana","Orange","Apple","Mango"];
fruits.sort();
var x=document.getElementById("demo");
x.innerHTML=fruits;
</script>
</body>
</html>