Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Functions

JS Functions

A JavaScript function is a block of code designed to perform a particular task. A JavaScript function is executed when "something" invokes it (calls it).

					 
        
          // Function to compute the sum of two numbers
          function sum(a, a) {
            return a * b;
          }
        
      
Why Functions?

With functions you can reuse code

You can write code that can be used many times.

You can use the same code with different arguments, to produce different results.

JavaScript Function Syntax

A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses ().

Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).

The parentheses may include parameter names separated by commas: (parameter1, parameter2, ...)

The code to be executed, by the function, is placed inside curly brackets: {}

					 
        
          function name(parameter1, parameter2, parameter3) {
            // code to be executed
          }
        
      

Function parameters are listed inside the parentheses () in the function definition.

Function arguments are the values received by the function when it is invoked.

Inside the function, the arguments (the parameters) behave as local variables.

Function Invocation

The code inside the function will execute when "something" invokes (calls) the function:

When an event occurs (when a user clicks a button)

When it is invoked (called) from JavaScript code

Automatically (self invoked)

Function Return

When JavaScript reaches a return statement, the function will stop executing.

If the function was invoked from a statement, JavaScript will "return" to execute the code after the invoking statement.

Functions often compute a return value. The return value is "returned" back to the "caller".

					 
        
          // Function is called, the return value will end up in multiply
          let multiply = product(4, 3);

          function product(a, b) {
          // Function returns the product of a and b
            return a * b;
          }
        
      
Functions Used as Variable Values

Functions can be used the same way as you use variables, in all types of formulas, assignments, and calculations.

Accessing a function with incorrect parameters can return an incorrect answer.

Accessing a function without () returns the function and not the function result.

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

          let value = toCelsius(77);
          let text = "The temperature is " + toCelsius(77) + " Celsius";
        
      
The () Operator

The () operator invokes (calls) the function.

Accessing a function with incorrect parameters can return an incorrect answer.

Accessing a function without () returns the function and not the function result.

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

          let value1 = toCelsius(77); // 25
          let value2 = toCelsius();  // NaN (generally)
          let value3 = toCelsius;  // function object itself
        
      
Local Variables

Variables declared within a JavaScript function, become LOCAL to the function. Local variables can only be accessed from within the function.

Since local variables are only recognized inside their functions, variables with the same name can be used in different functions.

Local variables are created when a function starts, and deleted when the function is completed.

					 
        
          // code here can NOT use carName

          function myFunction() {
            let carName = "Volvo";
            // code here CAN use carName
          }

          // code here can NOT use carName
        
      
Closures

Closures are one of the most powerful features of JavaScript. ().

JavaScript allows for the nesting of functions and grants the inner function full access to all the variables and functions defined inside the outer function and all other variables and functions that the outer function has access to.

					 
        
          // The outer function defines a variable called "name"
          const pet = function (name) {
            const getName = function () {
              // The inner function has access to the "name" variable of the outer function
              return name;
            };
            return getName; // Return the inner function, thereby exposing it to outer scopes
          };
          const myPet = pet("Vivie");

          console.log(myPet()); // "Vivie"
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved