JavaScript Functions

A Javascript functions are a group of statements that performs specific tasks. It is mainly used due to avoid code repetition and It is reused again and again as per requirement.

Function Declarations

Javascript function can be declared using function keyword followed by the function name, then parentheses (), and then code written inside curly brackets {}.

General Syntax
      function function_name(){
  //function statement
}
    
Try it now

Source Code

          
            function addNumber(){ 
          // code inside
}          
        
Try it now

Function Expression

General Syntax
      var addNumber=function(){ 
   // code inside 
};
    
Try it now

Source Code

          
            var addNumber=function(a,b){ 
     return (a+b);
 };          
        
Try it now

Calling a function

Javascript function can be called by function name followed by parenthesis, i.e ().

If a function name is addNumber, then use addNumber() to call the function.

General Syntax
      functionName(); //calling function
function functionName() {}
functionName(); //calling  function
    
Try it now

Source Code

          
            addNumber();
function addNumber() {}
addNumber();          
        
Try it now

Anonymous Expression

There is another way to create a function using function expression. In this, a variable is declared and then assigned an anonymous to that variable. Since this function does not have a name so it is also known as an anonymous function.

A javascript function expression is only invoked after the function. If function expression is called before the function then it generates an error.

General Syntax
      jsFunction();
var jsFunction = function () {};
jsFunction();
    
Try it now

Source Code

          
            greetMessage();
var greetMessage = function () {
  alert("Hello,i have learnt function.");
};
greetMessage();          
        
Try it now

Function Return Value

Javascript each function returns a value. Function default return value is undefined for all functions that do not contain a return statement. The function return value might be a string, number, function, array, or object.

Function returns undefined

Source Code

          
            function sayHello() {
  var x = "Hello ,i am learning javascript.";
}
sayHello(); // return undefined          
        
Try it now

Function returns string

Source Code

          
            function sayHello() {
  return "I am learning js .";
}
sayHello(); // return "hello"          
        
Try it now

Function Returns Number

General Syntax
      function functionName(a, b) {
  return a + b;
}
functionName(x, y);
    
Try it now

Source Code

          
            function addNumber() {
  return 10 + 50;
}
addNumber(); // return 60          
        
Try it now

Function returns function

The function can also return a function. Both Functions return a function and callback.

General Syntax
      function functionName() {
  return function () {
    return "something";
  };
}
functionName();
functionName()();
    
Try it now

Source Code

          
            function sayHello() {
  return function () {
    return "hello,javascript";
  };
}
sayHello(); // return function
sayHello()(); // return "hello,javascript"          
        
Try it now

Web Tutorials

Javascript Functions
Html Tutorial HTML
Javascript Tutorial JAVASCRIPT
Css Tutorial CSS
Bootstrap 5 Tutorial BOOTSTRAP 5
Bootstrap 4 Tutorial BOOTSTRAP 4