Javascript Create Function And Call It

Apr 20, 2021 . Admin

Hello Friends,

Now let's see example of how to create function and call it. We will use function and call it in javascript. Here you will learn how to create function and call it. This is a short guide on call the function. Let's get started with create function and call it in javascript.

Here i will give you many example how to create function and call it in.

Example: 1
<!DOCTYPE html>
<html>
	<head>
	   <title>Javascript - How to create function and call it?-MyWebtuts.com</title>
	</head>
	<body>

		<h2>JavaScript Functions</h2>

		<p id="test"></p>

		<script>

			var subject = {
			  	subjectName: function() {
			    	return this.firstSubject + " " + this.secondSubject;
			  	}
			}
			var subject1 = {
			  	firstSubject:"JavaScript",
			  	secondSubject: "Advance"
			}
			var subject2 = {
			  	firstSubject:"JavaScript",
			  	secondSubject: "Basic"
			}
			var x = subject.subjectName.call(subject1); 
			document.getElementById("test").innerHTML = x; 
		
		</script>

	</body>
</html>

Output: 1
JavaScript Advance
Example: 2 In this example i will use attribute for onclick event.
<!DOCTYPE html>
<html>
	<head>
	   <title>Javascript - How to create function and call it?-MyWebtuts.com</title>
	</head>
	<body>

		<h2>JavaScript Functions</h2>

		<p id="test"></p>

		<script>

			var dept = {
			  	fulldeptName: function(city, country) {
			    	return this.firstDept + " " + this.lastDept + "," + city + "," + country;
			  	}
			}
			var dept1 = {
			  	firstDept:"IT",
			  	lastDept: "Management"
			}
			var dept2 = {
			  	firstDept:"Science",
			  	lastDept: "physiotherapy"
			}
			var x = dept.fulldeptName.call(dept1, "Pune", "Baroda"); 
			document.getElementById("test").innerHTML = x; 

		</script>

	</body>
</html>
Output: 2
IT Management,Pune,Baroda

It will help you....

#Javascript