PROGRAMS

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

1) JavaScript Example : code between the body tag

<html>
<body>
<h2>Welcome to JavaScript</h2>
<script>
document.write("Hello JavaScript by JavaScript");
</script>
</body>
</html>

OUTPUT

Welcome to JavaScript
Hello JavaScript by JavaScript

2) JavaScript Example : code between the head tag


<html>
<head>
<script type="text/javascript">
function msg()
{
alert("Hello Javatpoint");
}
</script>
</head>
<body>
<p>Welcome to JavaScript</p>
<form>
<input type="button" value="click" onclick="msg()"/>
</form>
</body>
</html>
OUTPUT

Welcome to JavaScript
click

3. SINGLE LINE COMMENT


<html>
<body>
<script>
var a=10;
var b=20;
var c=a+b;//It adds values of a and b variable
document.write(c);//prints sum of 10 and 20
</script>
</body>
</html>

OUTPUT
30
4. MULTI LINE COMMENT
<html>
<body>
<script>
/* It is multi line comment.
It will not be displayed */
document.write("example of javascript multiline comment");
</script>
</body>
</html>

OUTPUT
example of javascript multiline comment
5. Create an external JavaScript file that prints Hello
Javatpoint in a alert dialog box.
message.js

function msg(){
alert("Hello Javatpoint");
}
index.html

<html>
<head>
<script type="text/javascript" src="message.js"></script>
</head>
<body>
<p>Welcome to JavaScript</p>
<form>
<input type="button" value="click" onclick="msg()"/>
</form>
</body>
</html>

OUTPUT

Welcome to JavaScript
Hello Javatpoint
6. Example of JavaScript variable
<html>
<body>
<script>
var x = 10;
var y = 20;
var z=x+y;
document.write(z);
</script>
</body>
</html>

OUPUT

30

7. JavaScript local variable


A JavaScript local variable is declared inside block or function. It is accessible within the
function or block only. For example:
<script>
function abc() {
var x=10;//local variable
}
</script>

Or,
<script>
If(10<13)
{
var y=20;//JavaScript local variable
}
</script>
8. JavaScript global variable
<html>
<body>
<script>
var data=200;//gloabal variable
function a(){
document.writeln(data);
}
function b(){
document.writeln(data);
}
a();//calling JavaScript function
b();

</script>
</body>
</html>

OUTPUT

200 200

9. JavaScript Global Variable


<html>
<body>
<script>
var value=50;//global variable
function a(){
alert(value);
}
function b(){
alert(value);
}

a();
</script>
</body>
</html>

OUTPUT
50

10. Declaring JavaScript global variable within


function
<html>
<body>
<script>
function m(){
window.value=100;//declaring global variable by window object
}
function n(){
alert(window.value);//accessing global variable from other function
}
m();
n();
</script>
</body>
</html>

OUTPUT
50
11. Simple example of if statement in javascript.
<html>
<body>
<script>
var a=20;
if(a>10)
{
document.write("value of a is greater than 10");
}
</script>
</body>
</html>

OUTPUT
value of a is greater than 10

12. JavaScript If...else Statement

<html>
<body>
<script>
var a=20;
if(a%2==0){
document.write("a is even number");
}
else{
document.write("a is odd number");
}
</script>
</body>
</html>

OUTPUT
a is even number
13. JavaScript If...else if statement
<html>
<body>
<script>
var a=20;
if(a==10){
document.write("a is equal to 10");
}
else if(a==15){
document.write("a is equal to 15");
}
else if(a==20){
document.write("a is equal to 20");
}
else{
document.write("a is not equal to 10, 15 or 20");
}
</script>
</body>
</html>

OUTPUT
a is equal to 20
14. Simple example of switch statement in javascript.
<html>

<body>
<script>
var grade='B';
var result;
switch(grade){
case 'A':
result="A Grade";
break;
case 'B':
result="B Grade";
break;
case 'C':
result="C Grade";
break;
default:
result="No Grade";
}
document.write(result);
</script>
</body>
</html>

OUTPUT
B Grade

15. behavior of switch statement in JavaScript.


<!DOCTYPE html>
<html>
<body>
<script>
var grade='B';
var result;
switch(grade){
case 'A':
result+=" A Grade";
case 'B':
result+=" B Grade";
case 'C':
result+=" C Grade";
default:
result+=" No Grade";
}
document.write(result);
</script>
</body>
</html>

OUTPUT

undefined B Grade C Grade No Grade B Grade C Grade No Grade

16. PROGRAM USING FOR LOOP

<html>
<body>
<script> OUTPUT
for (i=1; i<=5; i++) 1
{ 2
document.write(i + "<br/>") 3
} 4
</script> 5
</body>
</html>

17. JavaScript while loop


<html>
<body>
<script> OUTPUT
var i=11; 11
while (i<=15) 12
{ 13
document.write(i + 14 "<br/>");
i++; 15
}
</script>
</body>
</html>

18. JavaScript do while loop


<!DOCTYPE html>
<html>
<body>
<script> OUTPUT
var i=21; 21
do{
22
document.write(i + "<br/>");
23
i++;
}while (i<=25); 24
</script> 25
</body>
</html>

19. JavaScript Function Example


<html> Ccall function
<body>
<script>
function msg(){ hello! this is message
alert("hello! this is message");
}
</script>
<input type="button" onclick="msg()" value="call function"/>
</body>
</html>
20 . JavaScript Function Arguments
OUTPUT
<html>
<body> click
<script>
function getcube(number)
{
alert(number*number*number); 64

}
</script>
<form>
<input type="button" value="click" onclick="getcube(4)"/>
</form>
</body>
</html>

21. Function with Return Value


<html>
<body> OUTPUT
<script> hello javatpoint! How r u?
function getInfo(){
return "hello javatpoint! How r u?";
}
</script>
<script>
document.write(getInfo());
</script>
</body>
</html>
JavaScript Function Object
In JavaScript, the purpose of Function constructor is to create a new
Function object. It executes the code globally. However, if we call the
constructor directly, a function is created dynamically but in an unsecured
way.

Syntax
new Function ([arg1[, arg2[, ....argn]],] functionBody)

Parameter
arg1, arg2, .... , argn - It represents the argument used by function.

functionBody - It represents the function definition.

JavaScript Function Methods


Let's see function methods with description.

Method Description

apply() It is used to call a function contains this value and a single array of
arguments.

bind() It is used to create a new function.

call() It is used to call a function contains this value and an argument list.

toString() It returns the result in a form of a string.


22. JavaScript Function Object Examples
<!DOCTYPE html>
<html>
<body>

<script>
var add=new Function("num1","num2","return num1+num2");
document.writeln(add(2,5));
</script>

</body>
</html>

OUTPUT
7

Example 2
<!DOCTYPE html>
<html>
<body>

<script>
var pow=new Function("num1","num2","return Math.pow(num1,num2)");
document.writeln(pow(2,3));
</script>

</body>
</html>

OUTPUT
8
JavaScript Objects
A javaScript object is an entity having state and behavior (properties and
method). For example: car, pen, bike, chair, glass, keyboard, monitor etc.

JavaScript is an object-based language. Everything is an object in


JavaScript.

JavaScript is template based not class based. Here, we don't create class to
get the object. But, we direct create objects.

Creating Objects in JavaScript


There are 3 ways to create objects.

1. By object literal
2. By creating instance of Object directly (using new keyword)
3. By using an object constructor (using new keyword)

1) JavaScript Object by object literal


The syntax of creating object using object literal is given below:

object={property1:value1,property2:value2.....propertyN:valueN}

As you can see, property and value is separated by : (colon).


23. JavaScript Object by object literal
<html>
<body>
<script>
emp={id:102,name:"Shyam Kumar",salary:40000}
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>
</body>
</html>

OUTPUT
102 Shyam Kumar 40000

By creating instance of Object


The syntax of creating object directly is given below:

1. var objectname=new Object();

Here, new keyword is used to create object.


Let’s see the example of creating object directly.

<script>
var emp=new Object();
emp.id=101;
emp.name="Ravi Malik";
emp.salary=50000;
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>
24. By creating instance of Object

<html>
<body>
<script>
var emp=new Object();
emp.id=101;
emp.name="Ravi Malik";
emp.salary=50000;
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>
</body>
</html>

OUTPUT

101 Ravi Malik 50000

By using an Object constructor

Defining method in JavaScript Object


We can define method in JavaScript object. But before defining
method, we need to add property in the function with same name
as method.

The example of defining method in object is given below.


Here, you need to create function with arguments. Each argument
value can be assigned in the current object by using this keyword.

The this keyword refers to the current object.

The example of creating object by object constructor is given


below.

25. By using an Object constructor


<html>
<body>
<script>
function emp(id,name,salary){
this.id=id;
this.name=name;
this.salary=salary;
}
e=new emp(103,"Vimal Jaiswal",30000);

document.write(e.id+" "+e.name+" "+e.salary);


</script>
</body>
</html>

OUTPUT

103 Vimal Jaiswal 30000


26. Defining method in JavaScript Object
<html>
<body>
<script>
function emp(id,name,salary){
this.id=id;
this.name=name;
this.salary=salary;

this.changeSalary=changeSalary;
function changeSalary(otherSalary){
this.salary=otherSalary;
}
}
e=new emp(103,"Sonoo Jaiswal",30000);
document.write(e.id+" "+e.name+" "+e.salary);
e.changeSalary(45000);
document.write("<br>"+e.id+" "+e.name+" "+e.salary);
</script>
</body>
</html>

OUTPUT
103 Sonoo Jaiswal 30000
103 Sonoo Jaiswal 45000
JavaScript Array
JavaScript array is an object that represents a collection of similar
type of elements.

There are 3 ways to construct array in JavaScript

1. By array literal
2. By creating instance of Array directly (using new keyword)
3. By using an Array constructor (using new keyword)

1) JavaScript array literal


The syntax of creating array using array literal is given below:

1. var arrayname=[value1,value2.....valueN];

As you can see, values are contained inside [ ] and separated by ,


(comma).
Let's see the simple example of creating and using array in JavaScript.

27. JavaScript array literal

<html>
<body>
<script>
var emp=["Sonoo","Vimal","Ratan"];
for (i=0;i<emp.length;i++){
document.write(emp[i] + "<br/>");
}
</script>
</body>
</html>

OUTPUT

Sonoo
Vimal
Ratan

JavaScript Array directly (new keyword)


The syntax of creating array directly is given below:

1. var arrayname=new Array();

Here, new keyword is used to create instance of array.

Let's see the example of creating array directly.

28. JavaScript Array directly (new keyword)

<html>
<body>
<script>
var i;
var emp = new Array();
emp[0] = "Arun";
emp[1] = "Varun";
emp[2] = "John";
for (i=0;i<emp.length;i++){
document.write(emp[i] + "<br>");
}
</script>
</body>
</html>

OUTPUT
Arun
Varun
John

29. JavaScript array constructor (new keyword)


<html>
<body>
<script>
var emp=new Array("Jai","Vijay","Smith");
for (i=0;i<emp.length;i++){
document.write(emp[i] + "<br>");
}
</script>
</body>
</html>

OUTPUT

Jai
Vijay
Smith
30. JavaScript Digital Clock Example

<html>
<body>
Current Time: <span id="txt"></span>
<script>
window.onload=function(){getTime();}
function getTime(){
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
setTimeout(function(){getTime()},1000);
}
//setInterval("getTime()",1000);//another way
function checkTime(i){
if (i<10){
i="0" + i;
}
return i;
}
</script>
</body>
</html>

https://www.javatpoint.com/javascript-array

You might also like