1. 1. Write a program to display“HelloWorld!”.
<script type="text/javascript">
document.write("HelloWorld!");
</script>
Output
HelloWorld!
2. Write a program to findthe sumof any twonumbers
<script type="text/javascript">
var a=5, b=6, c=0;
c=a+b;
document.write("Firstvalue is="+a+"<br/>");
document.write("Secondvalueis="+b+"<br/>");
document.write("Total value is="+c+"<br/>");
</script>
Output
Firstvalue is=5
Secondvalue is=6
Total value is=11
3. Write a program to findthe total and percentage of anyfive subjects.
<script type="text/javascript">
var hindi=50,english=70,math=60, science=64,sst=65;
var total=0,percentage=0;
total=hindi+english+math+science+sst;
percentage=total*100/500;
document.write("Marksof englishis=",english,"<br/>");
document.write("Marksof mathis=",math,"<br/>");
document.write("Marksof science is=",science,"<br/>");
document.write("Marksof hindi is=",hindi,"<br/>");
document.write("Marksof sstis=",sst,"<br/>");
document.write("Total marksis=",total,"<br/>");
document.write("Percentage is=",percentage,"%<br/>");
</script>
Output
Marks of englishis=70
Marks of math is=60
Marks of science is=64
Marks of hindi is=50
Marks of sst is=65
2. Total marksis=309
Percentage is=61.8%
4. Write a programto findthe Area,
<script type="text/javascript">
var lenght=60,width=40,area=0;
area=lenght*width;
document.write("Lengthis=",length,"<br/>");
document.write("Widthis=",width,"<br/>");
document.write("Areais=",area,"<br/>");
</script>
Output
Lenghtis=60
Widthis=40
Areais=2400
5. Write a program to findthe (a+b)2
.
<script type="text/javascript">
var a=2, b=2, c=0;
c=a*a+b*b+2*a*b;
document.write("Valueof ais=",a,"<br/>");
document.write("Valueof bis=",b,"<br/>");
document.write("(a+b)<sup>2</sup>is=",c,"<br/>");
</script>
Output
Value of a is=2
Value of b is=2
(a+b)2
is=16
6. Write a program to findthe simple Interest.
<script type="text/javascript">
var amount=5000,rate=3,time=5,si=0,total=0;
si=amount*rate*time/100;
total=amount+si;
document.write("Amountis=",amount,"<br/>");
document.write("Rate is=",rate,"<br/>");
document.write("Time is=",time,"<br/>");
document.write("Simple Interestis=",si,"<br/>");
document.write("Total amountis=",total,"<br/>");
</script>
Output
Amountis=5000
Rate is=3
3. Time is=5
Simple Interestis=750
Total amountis=5750
7. Write a program to findthe givennumberisEvenorOdd.
<script type="text/javascript">
var number=8;
document.write("Givennumberis=",number,"<br/>");
if(number%2==0)
{
document.write("Even");
}
else
{
document.write("Odd");
}
</script>
Output
Givennumberis=8
Even
8. Write a program to findthe givennumberisEvenorOdd and Zero.
<script type="text/javascript">
var number=8;
document.write("Givennumberis=",number,"<br/>");
if(number>0)
{
document.write("+ve");
}
else
if(number<0)
{
document.write("-ve");
}
else
{
document.write("zero");
}
</script>
Output
Givennumberis=8
+ve
9. Write a program to findthe largestbetweentwonumbers.
<script type="text/javascript">
4. var a=9,b=10;
document.write("Valueof ais=",a,"<br/>");
document.write("Valueof bis=",b,"<br/>");
if(a>b)
{
document.write("A isgreaternumber");
}
else
if(b>a)
{
document.write("Bisgreaternumber");
}
else
{
document.write("Bothare equal");
}
</script>
Output
Value of a is=9
Value of b is=10
B is greaternumber
10. Write a program to findthe largestbetweenthreenumbers.
<script type="text/javascript">
var a=9,b=6,c=2;
document.write("Valueof ais=",a,"<br/>");
document.write("Valueof bis=",b,"<br/>");
document.write("Valueof cis=",c,"<br/>");
if(a>b&& a>c)
{
document.write("A isgreaternumber");
}
else
if(b>a&& b>c)
{
document.write("Bisgreaternumber");
}
else
if(c>a&& c>b)
{
document.write("Cisgreaternumber");
}
else
{
document.write("Equal");
}
5. </script>
Output
Value of a is=9
Value of b is=6
Value of c is=2
A isgreaternumber
11. Write a program to findthe ticketcostaccording to age:if age<=1) ticketcost is50 Rupees ,if
age>15 and age <=30 ticketcostis 100 Rupees,if age >30 ticketcost is150 Rupees.
<script type="text/javascript">
var age=14, tc=0;
document.write("Ageis=",age,"<br/>");
if(age<=15)
{
tc=50;
}
else
if(age>15&&age<=30)
{
tc=100;
}
else
if(age>30)
{
tc=150;
}
document.write("Ticketcostis=",tc,"<br/>");
</script>
Output
Ticketcost is=50
12. Write a program to findthe ProfitandLose according to the Costprice and Sale price.
<script type="text/javascript">
var cp=80,sp=80;
document.write("Costprice is=",cp,"<br/>");
document.write("Sale price is=",sp,"<br/>");
if(sp>cp)
{
12. {
var i=0,f=1;
var n=parseInt(document.getElementById("n").value);
while(i<n)
{
i=i+1;
f=f*i;
}
alert("Factorial numberis="+f);
}
</script>
</head>
<body>
Enter anynumber: <inputtype="text"value="4"id="n">
<inputtype="button"value="Factorial"onclick="fact();">
</body>
</html>
Output
32. Write a program to show the febnocci series
<html>
<head>
<title>Febnocci series</title>
<script type="text/javascript">
var a,b,c,n=10;
document.write("Givennumberis=",n,"<br/>");
a=1;
b=1;
document.write(a,"<br/>");
document.write(b,"<br/>");
for(c=1;c<=n;c++)
{
c=a+b;
a=b;
b=c;
document.write(c,"<br/>");
13. }
</script>
</head>
<body>
</body>
</html>
The JavaScript Switch Statement
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var day=2;
switch(day) {
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
break;
case 7:
day="Sunday";
break;
default:
day="Wrongday";
break;
}
document.getElementById("demo").innerHTML
= "Todayis " + day;
</script>
<body>
</body>
</html>
In javascriptwrite aprogram using“switchcase”to accept two numbersfromuserand perform
addition,subtractionandproducttothe acceptednumber.
<!DOCTYPE html>
<html>
<body>
<script>
var a=0,b=0,ans=0,ch=0;
a=Number(prompt("Enterfirstvalue="));
b=Number(prompt("Entersecondvalue="));
ch=Number(prompt("Press1.(addition) 2.
(subtraction),3.(product)="));
switch(ch)
{
case 1:
ans=a+b;
break;
case 2:
Output
Givennumberis=10
1
1
2
3
5
8
13
14. ans=a-b;
break;
case 3:
ans=a*b;
break;
default:
alert("Wrongchoice");
break;
}
document.write("Firstvalue is=",a,"<br/>");
document.write("Secondvalueis=",b,"<br/>");
document.write("Resultis= ",ans,"<br/>");
</script>
<body>
</body>
</html>
Write a program inhtml usingjava scriptto accept anytwo numbersfromuseran performfollowing
operationinthe same program.
Sumof the numbers
Difference betweenthe numbers(subtraction)
Productsof the numbers
<!DOCTYPE html>
<html>
<body>
<script>
var a=0,b=0,sum=0,sub=0,pro=0;
a=Number(prompt("Enterfirstvalue="));
b=Number(prompt("Entersecondvalue="));
sum=a+b;
sub=a-b;
pro=a*b;
document.write("Firstvalue is=",a,"<br/>");
document.write("Secondvalue
is=",b,"<br/><br/>");
document.write("Sumis=",sum,"<br/>");
document.write("Subtractionis=",sub,"<br/>");
document.write("Productis=",pro,"<br/>");
</script>
<body>
</body>
</html>
Write a program inhtml usingjavascriptto declare afunction‘fact()’calculate factorial of entered
numberusingthisfunction.
<!DOCTYPE html>
<html>
<body>
<script>
function fact()
{
var i=0,f=1;
var
n=parseInt(document.getElementById("n").valu
e);
var ans=document.getElementById("ans");
while(i<n)
{
i=i+1;
f=f*i;
}
ans.value=f;
}
</script>
<body>
Enter anynumber: <inputtype="text"value=""
id="n">Factorial numberis: <inputtype="text"
value=""id="ans"><inputtype="button"
value="Result"onclick="fact();">
</body>
</html>
Output
15. Break
The break statement breaks the loop and continues executing the code after the
loop
<script>
var i=0;
for (i = 1; i <=10; i++) {
if (i == 5)
{
break;
}
document.write(i,"<br/>");
}
</script>
Output
1
2
3
4
Data typesinjavascript
o Boolean
o Null
o Undefined
o Number
o String
Boolean
Boolean represents a logical entity and can have two values: true, and false.
var x = 5;
var y = 5;
var z = 6;
(x == y) // Returns true
(x == z) // Returns false
Null
The Null type has exactly one value: null.
<script>
var n;
if(n==null)
{
document.write('Not value found');
}
</script>
16. Undefined
In JavaScript, a variable without a value, has the value undefined. The typeof is
also undefined.
var car; // Value is undefined, type is undefined
Number
JavaScript has only one type of numbers.
Numbers can be written with, or without decimals:
var x1 = 34.00; // Written with decimals
var x2 = 34; // Written without decimals
String
o A string (or a text string) is a series of characters like "John Doe".
o Strings are written with quotes. You can use single or double quotes:
var carName = "Volvo XC60"; // Using double quotes
var carName = 'Volvo XC60'; // Using single quotes
There are 3 JavaScript methods that can be used to convert variables to numbers:
The Number() method
The parseInt() method
The parseFloat() method
Method Description
Number() Returns a number, converted from its
argument.
parseFloat() Parses its argument and returns a floating
point number
parseInt() Parses its argument and returns an integer
Math.round()
Math.round(x) returns the value of x rounded to its nearest integer:
Math.round(4.7); // returns 5
Math.round(4.4); // returns 4
Math.pow()
Math.pow(x, y) returns the value of x to the power of y:
17. Math.pow(8, 2); // returns 64
Math.sqrt()
Math.sqrt(x) returns the square root of x:
Math.sqrt(64); // returns 8
Math.abs()
Math.abs(x) returns the absolute (positive) value of x:
Math.abs(-4.7); // returns 4.7
Math.ceil()
Math.ceil(x) returns the value of x rounded up to its nearest integer:
Math.ceil(4.4); // returns 5
Math.ceil()
Math.ceil(x) returns the value of x rounded up to its nearest integer:
Math.ceil(4.4); // returns 5
Math.min() and Math.max()
Math.min() and Math.max() can be used to find the lowest or highest value in a list
of arguments:
Math.min(0, 150, 30, 20, -8, -200); // returns -200
Math.max(0, 150, 30, 20, -8, -200); // returns 150
Math.random()
Math.random() returns a random number between 0 (inclusive), and 1 (exclusive):
Math.random(); // returns a random number
Using String search() With a Regular Expression
18. Use a regular expression to do a case-insensitive search for "w3schools" in a string:
var str = "Visit W3Schools";
var n = str.search(/w3schools/i);
6
Use String replace() With a Regular Expression
Use a case insensitive regular expression to replace Microsoft with W3Schools in a
string:
var str = "Visit Microsoft!";
var res = str.replace(/microsoft/i, "W3Schools");
Visit W3Schools!
var str = "Visit Microsoft!";
var res = str.replace("Microsoft", "W3Schools");
Modifier Description
i Perform case-insensitive matching
g Perform a global match (find all matches rather than stopping
after the first match)
m Perform multiline matching