Functions in javascript

 



What is a Function in JavaScript

A JavaScript function is a block of code designed to perform a particular task. In simple terms, Functions are used to make work easy, reduce the effort of coding multiple lines for the same task which is needed to repeat multiple times.

There are three parts of the Function.

  1. The keyword function with the function name.
  2. Function parameter (if it is parameterized).
  3. Function body (Contains the instructions part).
  4. Return statement.

There are three steps of Function to use in the program.

  1. Function Declaration
  2. Function Definition.
  3. Function Call

In JavaScript function declaration and definition are done together.

The syntax for function Declaration & Definition.

  1. We need to declare the function name starting with the keyword function.
  2. After that, we need to open and close the parenthesis (to pass the parameter/s).
  3. Then we need to open the brackets (to write the function body).
  4. In the last line, we need to put the return statement.
  5. Now we need to close the brackets.

An example of function definition and declaration is shown below:-

1
2
3
function toCelsius(fahrenheit) {
  return (5/9) * (fahrenheit-32);
}

Different ways for function Call

There are four ways we can call the function in JavaScript.

  1. As a function (we can use the log function)
1
2
3
function sayHello() {
      console.log('hello');
    }

2. As a method (we can use function name followed by . then the method name )

1
2
3
4
5
6
document.getElementById("demo").inner HTML = "Hello! I called myself";
or
function_name.object_name();
or with call method
function_name.object_name.call();
and many more.

3. As a constructor (In sayHello function, we grab the value in the text box and create a new instance of the Greeter object by calling new Greeter().)

1
2
3
4
5
6
7
8
9
10
11
12
function Greeter(name) {
      console.info('begin constructor');
      console.log(this);
      console.log(arguments);
      this.name = name;
      console.info('end constructor')
    }
    function sayHello() {
      var name = document.getElementById('name').value;
      var grtr = new Greeter(name);
      console.log('hello ' + grtr.name);
    }

4. via call() and apply()



Few codes for function in JavaScript:-



We could also use Functions as Variable Values

1
2
3
function toCelsius(fahrenheit) {
  return (5/9) * (fahrenheit-32);
}

Instead of using a variable to store the return value of a function:

1
2
var x = toCelsius(77);
var text = "The temperature is " + x + " Celsius";

We can use the function directly, as a variable value

1
var text = "The temperature is " + toCelsius(77) + " Celsius";


Few important links for your reference:-

For understanding a brief description of JAVASCRIPT click here

For understanding variables in JavaScript (var/let/const) click here

For understanding, registration-form-validation-using-javascript click here

You Might Also Like

0 comments