How do javascript functions differ from Java functions?

How do you avoid repeated coding in a programming language?

By using functions.

You write the piece of code with the logic you want to execute inside a function and then call it wherever you want.

Javascript functions achieve the same but differ a bit from languages like Java.

Here are the features in javascript that you don’t have in Java .

You don’t specify the type of javascript parameters:

Here is how you can define a function in Javascript:

function greet(name){

   return "Hello "+name;

}

The parameter name does not have a type whereas in Java you need to represent the above function like this:

function greet(String name){
  return "Hello "+name;

}

Javascript functions don’t need to live inside a Class:

Java functions cannot exist outside a class, but javascript functions can be present outside a class and that is often the norm.

Javascript functions can be assigned to a variable:

This might sound weird to a java developer but javascript functions can be assigned to a variable and that variable can then be used to invoke the function.

The above function can be assigned to a variable like this:

var greet = function(message){  

   return "Hello "+message;

}

Notice that the function name has been removed following the function keyword and the variable greet is representing that function.

You can call the above function just like before :

greet("Ram")

Javascript functions can be passed as parameters to other functions:

Passing a function to another function.

That would have sounded strange to a Java developer until lamdas were introduced in Java 8.

It was always possible in javascript though.

You can pass a function as a parameter to another function.

You can then execute the passed function inside the called function.

Consider the below example:

add(2,function(x,y){return x+y});

I am calling a function named add() and passing a function as the second parameter to the first function.

Here is the function add():

function add(value, anotherfunction) {
    

    var secondsum = anotherfunction(3,4);

    return value + secondsum;
}

As you can see , the passed function is invoked inside the function by passing the paramters 3 and 4.

The passed function then gets executed with these parameters and returns the sum of the two values which is 7.

This is again added to the first parameter of the original function (the value 2) and the final value of 9 (7+2) is returned by the original function.

You can also declare the function passed as parameter separately and pass it:

function anotherfunction(x,y){

   return x  + y;

}

add(2,anotherfunction);

It works the same way!

Notice there are no paranthesis when the function anotherfunction is passed as parameter.

You can pass any number of parameters to a javascript function:

Java strictly checks if the number of parameters passed to a function matches the number of parameters declared. If it doesn’t it doesn’t compile.

But javascript doesn’t .

You can pass any number of arguments to a javascript function.

If you pass more parameters , the additional parameters will be ignored.

If you pass less parameters , the missing parameters will be represented as undefined.

And you can use the arguments keyword to know the arguments.

The below code :

function add(x,y){

    console.log(x,y);

    console.log(arguments);


}

add(3,5,6);

produces the below output in the console:

And calling the add function with a single parameter like this:

add(3);

produces the below output:

Notice the word undefined for in the first line which represents the missing parameter.

If you have different functions with the same name and different number of parameters , the appropriate function will be called. For example if you call the function with a single parameter and there is a function with a single argument that will be called.

That’s it!


Posted

in

,

by

Comments

One response to “How do javascript functions differ from Java functions?”

  1. Lee Valdez Avatar

    Grreat read thankyou

Leave a Reply

Discover more from The Full Stack Developer

Subscribe now to keep reading and get access to the full archive.

Continue reading