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

Javascript Programs

Uploaded by

Nikhil Loverz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Javascript Programs

Uploaded by

Nikhil Loverz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

JAVASCRIPT PROGRAMS

Q1. Write a Javascript Program to show Hello Javascript on web


page?

<html>

<body>

<h1>my first web page</h1>

<script>

document.write(“Hello Javascript”);

</script>

</body>

</html>

Q2. Write a Javascript Program to show How to use variable?

<html>

<body>

<h1>my first web page</h1>

<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>

var d=new Date();

document.write(d);

</script>

</body>

</html>

Q4. Write a Javascript Program to show Pop up boxes (alert, confirm


& prompt)?
I. Alert

<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>

Welcome to alert box.

</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>

Welcome to confirm box.

</body>

</html>

III. Prompt

<html>

<body>

<p><strong>Click the button to demonstrate the prompt


box.</strong></p>

<button onclick="myFunction()">try it</button>

<p id="demo"></p>

<script>

function myFunction()
{

var x;

var person;

person=prompt("please enter your name"," ");

if(person!=null)

x=("hello" +person+ "!how are u today?");

document.getElementById("demo").innerHTML=x;

</script>

</body>

</html>

OR

<html>

<head>

<script>

prompt("HELLO! What is your name? ");

</script>

</head>

<body>

Welcome to prompt box.

</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>

Q6. Write a Javascript Program to show how to use function with an


argument?

<html>

<body>

<p>click the button to call a function with arguments</p>

<button onclick="myFunction('The Earth','around the Sun')">try it</button>

<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()

return("Have a nice day!!!");

</script>

</head>

<body>

<script>

document.write(myFunction());

</script>

</body>

</html>

Q8. Write a Javascript Program to show Function with arguments that


returns a value.?

<html>

<body>

<p>Function with arguments that returns the result</p>

<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>

<p>Length of the given string =

<script>

var txt="Hello world";

document.write(txt.length);

</script>

</p>

</body>

</html>

Q10. . Write a Javascript Program to find the position of the first


occurrence of a text in a string using indexOf()

<html>

<body>

<p id="demo"><strong>click the button to locate where in the string a


specified value

occurs.</strong></p>

<button onclick="myFunction()">try it</button>

<script>

function myFunction()

var str="hello! welcome to my world";


var n=str.indexOf("world");

document.getElementById("demo").innerHTML=n;

</script>

</body>

</html>

Q11. Write a Javascript Program to search for a text in a string and


return the text if found using match() ?

<html>

<body>

<script>

var str="Honesty is the best policy";

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>

<p>click the button to replace the characters</p>

<p id="demo">hello prachi</p>

<button onclick="myFunction()">try it</button>

<script>

function myFunction()

var str=document.getElementById("demo").innerHTML;

var n=str.replace("hello","good morning");

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>

<button onclick="myFunction()">try it</button>

<script>

function myFunction()

document.getElementById("demo").innerHTML=Math.round(8.7896);

</script>

</body>

</html>

Q14. Write a Javascript Program to return a value random number


between 0 and 1 using random() ?

<html>

<body>

<p id="demo">click the button to display a number</p>

<button onclick="myFunction()">try it</button>

<script>

function myFunction()

document.getElementById("demo").innerHTML=Math.random();
}

</script>

</body>

</html>

Q15. Write a Javascript Program to return the number with highest


value of two specified numbers using max()?

<html>

<body>

<p id="demo">Click the button to return the highest no. between 5 and
10.</p>

<button onclick="myFunction()">try it</button>

<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>

Array method Programs

Q17. Write a Javascript Program to create an array ?

<html>

<body>

<script>

var i;

var fruits=new Array();

fruits[0]="apple";

fruits[1]="banana";

fruits[2]="orange";

for(i=0;i<fruits.length;i++)

{
document.write(fruits[i]+"<br>");

</script>

</body>

</html>

Q18. Write a Javascript Program to join two arrays using concat() ?

<html>

<body>

<p id="demo">Click the button to join three arrays</p>

<button onclick="myFunction()">Click me</button>

<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>

<p id="demo">Click the button to remove the last array element.</p>

<button onclick="myFunction()">Click me</button>

<script>

var name=["John","Mary","Oliver","Alice"];

function myFunction()

name.pop();

var x=document.getElementById("demo");

x.innerHTML=name;

</script>

</body>

</html>

Q20. Write a Javascript Program to add a new element to the array


using push()?

<html>

<body>

<p id="demo">Click the button to add a new element to the array.</p>

<button onclick="myFunction()">Click me</button>

<script>
var name=["Alice","Henry","John","Leo"];

function myFunction()

name.push("Aayush");

var x=document.getElementById("demo");

x.innerHTML=name;

</script>

</body>

</html>

Q21. Write a Javascript Program to reverse the order of the elements


in the array?

<html>

<body>

<p id="demo">Click the button to reverse the order of the element in the
array.</p>

<button onclick="myFunction()">Click me</button>

<script>

var alphabet=["z","k","j","h","e"];

function myFunction()

alphabet.reverse();

var x=document.getElementById("demo");

x.innerHTML=alphabet;
}

</script>

</body>

</html>

Q22. Write a Javascript Program to sort the array?

<html>

<body>

<p id="demo">Click the button to sort the array</p>

<button onclick="myFunction()">Click me</button>

<script>

function myFunction()

var fruits=["Banana","Orange","Apple","Mango"];

fruits.sort();

var x=document.getElementById("demo");

x.innerHTML=fruits;

</script>

</body>

</html>

You might also like