Sharp Tutorial
function in js

Python

Java

C

C++

HTML/CSS

Java Script

PHP

SQL

C Programs

Java Script

  • java script home
  • introduction
  • Variable Declare & Make Comment
  • operator
  • if else
  • loop
  • Array
  • Function
  • Java Script Objects
  • Java Script DOM

JS based examples

  • two number sum and diff programs
  • Pattern program in java script
  • zumble word quiz
  • Random number guess program
  • calculator in java script
  • digital clock in java script

HTML

  • HTML Home
  • Introduction
  • Commonly used tags
  • Formatting
  • Working with class
  • Apply Css
  • Layout
  • Link / Image
  • Html Forms
  • Html Tables
  • Responsive Design

CSS

  • Introduction
  • Types of Css
  • Background and Color
  • Css Formatting
  • Css Layout
  • Css Animation
  • Css Table
  • Css Effects
  • Css Pagination
  • Css Media Query

Java Script Function

in java script a function is declared with keyword function followed by parentheses .Function may contain a parameter . The code of function executes when function is being called.

In JS function may be declared as following. In following example the function is being called on onLoad event . So it executes when we open the web page.

 


function MyFunction()
{
alert("This is Simple Function");
}

We can create n number of functions as per our requirements only called function will be executed. A function may also have parameter so when we call the function values as parameter have to be passed

JS function may also return the value for returning the value at the end of the function we have to give return statement.

 

<html>
<title>JS introduction</title>
<head>
<script>
function MyFunction()
{
alert("This is Simple Function");
}
</script>
</head>
<body onLoad="MyFunction()">
</body>
</html>

    Output: This is simple function in alert pop up

In following example we have two functions we call MainFunction() on page load . MainFunction also calls another function SumFunction() which takes two values and return the sum after calculation. Main function receives the value and print it.

 

<html>
<title>JS introduction</title>
<head>
<script>
function SumFunction(n1,n2)
{
var n=n1+n2;
return n;
}
function MainFunction()
{
var k=SumFunction(10,20);
alert("The sum is"+k);
}
</script>
</head>
<body onLoad="MyFunction()">
</body>
</html>

    Output: The sum is 30

[codepen_embed height="265" theme_id="default" slug_hash="oNgzPmY" default_tab="js,result" user="rajputadesh"]See the Pen oNgzPmY by rajput adesh (@rajputadesh) on CodePen.[/codepen_embed]

Enquiry about Course

Ask your Question

Click Here

Sharp (2) Tutorials

Video/ C Introduction

Watch video in full size

Video tutorial

Follow by Email
Facebook
Facebook
fb-share-icon
YouTube
Copyright © Sharp Tutorial
Build with WordPress
Wordpress Social Share Plugin powered by Ultimatelysocial
Sharp Tutorial