Writing a function in Javascript

What is a function

It is a way of writing a set of logic separately from the rest of the code. Functional coding is a clean, readable and reusable approach of programming.

It is also assigned a name for the ease of accessibility. Can we write functions without names? The answer is yes. We call those anonymous functions. I have discussed it later in this article.

Declaration of a function in Javascript

function name(parameter1, parameter2, ....) {
  statement1
  statement2

}

How to write a function in Javascript

A function declaration has three parts:

  • Name
  • Parameters
  • Statements

Name

A function is given a name so that we can identify it easily. Naming reduces ambiguity among multiple functions. A name gives us a quick idea about what the function does.

It saves a lot of time since we don’t need to read the whole logic again and again before using it.

Function names are case sensitive. fooBar and Foobar will call different functions. Make sure to name it in the most descriptive ways possible.

Some of the naming examples are below.

sendErrorMessage();
send_error_message();
senderrormessage();

The function name is optional. We decide to name a function, depending upon its use. I have described it in more detail in a later section.

Parameters

Sometimes the inner statements of a function may require some external input value.

For example, I am writing a function Add, that will return me the addition of two numbers. Now, there must be some way of passing these two numbers into the function.

Using parameters we do that.

function Add(firstNumber, secondNumber) {
  return firstNumber  + secondNumber;
}

Add(212, 314); 
// 526
Add("Hi", "There"); 
//HiThere

Yes, the second function call will return a concatenated string if we don’t put a number check for those passed parameters. The plus operator concatenates strings. It’s a feature of javascript.

A function can accept parameters depending on its declaration. There are provisions of handling the case when we don’t pass the sufficient number of parameters in a function call.

Parameters can be of any types available in javascript. String, number, array, object and we can even pass a whole function as a parameter of another function.

Statements

Statements are the logic that runs inside the function. It can be a member declaration, business rules, return statement etc.

Scope of a function in javascript

A function has access to its variables, own methods, global variables, global methods, variables and methods declared in its parent function etc.

Sibling functions don’t have access to the scope of each other. A parent function can’t access the scope of its child function.

var globalA = 2;

function parent() {

  var parentA = 3;

  function child() {

    var childA = 5;
    console.log(globalA ); //2
    console.log(parentA ); // 5
  }

  function secondChild() {

    console.log(parentA ); // 5
    console.log(childA ); // undefined
  }
}

You can read more about functions in my blog

19