Call Functions in JavaScript | Chris Pietschmann

One of the fundamental tasks with JavaScript is how to write and call functions in your code. This may seem like a simpleton thing, and it basically is, however there are a few tips and tricks you can use to help making your manipulation of JavaScript Functions easier and more intuitive. This article takes you through the basics of calling functions in your code, and some more advance tips and tricks to writing better JavaScript functions that can help take your JavaScript tease to the next degree .

How to call a function in JavaScript

Calling a function ( aka method ) in JavaScript is exchangeable to any early program language that uses a C-like syntax. Simply call the function by name, then pass in any want parameters in a comma delimited list enclosed in parenthesis .
To put this in a great model that can be easily visualized, let ’ s take the play along sayHello function that accepts a argument named name of type string :

 routine   sayHello ( name )  {
     // execution code here

To call this affair, you plainly call it immediately by name, then pas in the parameter value :

 sayHello ( `` Chris `` ) ;

nowadays, if you have a function you need to call that doesn ’ triiodothyronine have any parameters, you can call it similarly. A function without any parameters can be called by omitting any parameters, and call it the like direction :

 sayHello ( ) ;

If the serve accepts multiple parameters, then you can call it similarly with all the parameter values as a comma delimited number :

 sayHello ( `` Chris ``,  `` Pietschmann ``,  42 ) ;

This former model accepts three parameters. The first gear two are string values, while the third parameter accepts a number .

Call a JavaScript function with return value

While parameters are used to pass one or more values into a JavaScript function, you can use a reelect value to pass a value out of the affair american samoa well. The function can use the return statement to exit and pass some prize back to the caller when it ’ second completed execution .
here ’ s an example of a JavaScript officiate with a come back value :

 function  multiply ( a,  bel )  {
     // simple exemplar that multiplies two numbers
     volt-ampere  c  =  a  *  bel ;
     return  c ;

This function can be called using the lapp method as any other function. By precisely calling it the like room, you will just ignore the rejoinder value, and move on .

 // address function and ignore return value
 reproduce ( 6,  7 ) ;

If you need to capture the come back prize from the affair, then you can assign it ’ s resultant role to a variable. The variable will then contain the value returned from the function after murder .

 volt-ampere  c  =  multiply ( 6,  7 ) ;

once the return value has been captured into a variable, that variable can then be used like any other variable star. You can consume it ’ s rate immediately in this code for something, or even pass it as a parameter to another function .
If the return value of a affair is entirely going to be used as an stimulation argument of another routine, you can pass the render measure directly to the function parameter. This is done by putting the function predict in the parameters number of the other officiate address, just like you would a varying .
here ’ s an exercise of passing a variable star as a parameter to a function call, and another that passes the return value of a serve directly to the parameters of another function :

 // pass return key value as argument to another affair birdcall
 volt-ampere  c  =  breed ( 6,  7 ) ;
 volt-ampere  five hundred  =  reproduce ( c,  8 ) ;

 // spend return value directly as parameter to another function address
 volt-ampere  vitamin d  =  breed ( reproduce ( 6,  7 ),  8 ) ;
 // here 's the same broken out to multiple lines for legibility
 volt-ampere  d  =  multiply (
     reproduce ( 6,  7 ) ,
 ) ;

Passing Functions as Parameters

JavaScript is a functional program speech. This means that functions are variables excessively. As a leave, you can pass functions around as parameters to other functions excessively. This offers some bang-up tractability benefits for code recycle, recursion, and early functional programming benefits .
here ’ s an model of passing a affair as a parameter to another function that builds on the above multiply(a,b) function examples :

 // function that accepts a routine as an remark argument
 routine  doMath ( process,  a,  b-complex vitamin )  {
     // call the parameter serve
     // and return the result
     render  operation ( a,  b ) ;

 // permit 's call this function
 volt-ampere  c  =  doMath ( multiply,  6,  7 ) ;

JavaScript Object Methods

Getting a small more object oriented ( full JavaScript OOP is discussion for another article ), you can have a JavaScript object with it ’ second own properties and methods. These methods are functions besides. The occupy on the object, and when called immediately have access to the object using the this keyword. The this keyword can be used to reference the object and it ’ s properties .
here ’ s an exercise of defining a JavaScript object that has some properties and methods :

 // object definition
 volt-ampere  author  =  {
     firstName :  `` Chris `` ,
     lastName :  `` Pietschmann `` ,
     favoriteNumber :  2063 ,

     getFullName :  function  ( )  {
         restitution  this. firstName  +  ``  ``  +  this. lastName ;
 } ;

 // call the getFullName method on the object
 volt-ampere  fullName  =  author. getFullName ( ) ;
 // return value is `` Chris Pietschmann ''

Invoke JavaScript Functions using the call() method

If you have a JavaScript object that has it ’ s own properties and methods, you can call besides call those methods using the call() method. The call() method acting enables you to call a function by passing in the context for the this keyword within the function, and any ask parameters. This enables you to scope the this keyword for the method acting to reference the object .
here ’ s an example of using the call() method to call the getFullName method of the above author object :

 volt-ampere  fullName  =  generator. getFullName. call ( author ) ;

here ’ s an model of defining a fresh officiate that doesn ’ triiodothyronine exist as a method of the object, then calling it with the context of the this keyword to the object .

 function  doubleFavoriteNumber ( )  {
     revert  this. favoriteNumber  *  2 ;

 volt-ampere  a  =  doubleFavoriteNumber. call ( generator ) ;

The call() method acting accepts the this context to call the method with, in summation to any parameters needed for that function. You can pass these parameters to the function by appending these parameter values to the parameters list sent to the call() method .
here ’ s another exemplar that accepts a parameter value :

 officiate  doubleFavoriteNumber ( newton )  {
     return  this. favoriteNumber  *  north ;

 volt-ampere  a  =  doubleFavoriteNumber. call ( writer,  2 ) ;

Function as Return Value of Function

This may sound confusing at first. A JavaScript function can be passed as a return measure from another function. This can lead to some more advance scenarios, and is useful to know is possible. specially if you ’ re consuming a JavaScript library or NPM package that has methods / functions that return functions as a consequence to be consumed later .
here ’ s an example of a officiate definition that returns a function as the return rate from the function itself :

 routine  getFullName ( fName,  lName )  {
     return  routine  ( )  {
         rejoinder  fName  +  ``  ``  +  lName ;

 // call the routine
 volt-ampere  func  =  getFullName ( `` Chris ``,  `` Pietschmann `` ) ;

now calling this officiate will return a function, not any other type of value. The previous case doesn ’ t return "Chris Pietschmann", but quite a function can when called will return that. You can then call this routine when needed to generate that prize ;
now, let ’ s call the function that was returned from the other function to get the final examination solution returned :

 volt-ampere  fullname  =  func ( ) ; 


Calling JavaScript functions is simpleton at first gear, but you can see there are a number of functional program techniques that can be used. When you better understand that a JavaScript Function is an aim, alike to strings and numbers, then you have the cognition to write better JavaScript code .
obviously these techniques help if you ’ re writing a new JavaScript libarary, model, or NPM box. however, knowing these techniques will besides help you to better consume other libraries, frameworks, and NPM packages in your own code .
happy script !

source :
Category : Tutorial

Trả lời

Email của bạn sẽ không được hiển thị công khai.