Skip to content
Related Articles

Related Articles

Improve Article
JavaScript Course | Functions in JavaScript
  • Last Updated : 29 Apr, 2019

Previous Topic: JavaScript Course | Loops in JavaScript
Javascript functions are code blocks that are mainly used to perform a particular function. We can execute a function as many times we want by calling it(invoking it).
To create a function, we use function() declaration, like:

// anonymous function
function(){
 // function...body
}

// function with a name
function displayMessage(){
 // function..body
}

Functions in javascript can be both anonymous and named, it totally depends on who’s writing them. We make use of the name when we want to call(invoke) the function and use the value that the functions return.
Example:




<script>
function displayMessage(){
 alert('How you doin'?');
}
// calling the function twice
displayMessage();
displayMessage();
</script>

In the above code we simply wrote a function which creates a simple alert pop-up with a simple message inside it and the way we run it is by calling the function name.

Local Variable
A Variable which is declared inside the function body is only available inside the function block.
Example:




<script>
// local variable example
function displayMessage(){
  let message = 'This is the message';
  alert(message);
}
displayMessage();
  
alert(message); // gives an error
</script>                    

In the above code, we declared a function named displayMessage() and then inside it declared a variable and then printed that variable and also tried to print that variable outside the function block. Trying to use of variable outside of its declared block will result in an error.



Outer Variable
Outer variables allows us to use them inside the function code block and also outside it.
Example:




<script>
// Outer variable
let message = 'This is the message';
function displayMessage(){
  alert('Listen '+message);
}
  
displayMessage();
alert(message);
</script>

Let’s see another example where we learn about how the function can modify the message as well.
Example:




<script>
// modiying outer variable
let message = 'This is a message';
function displayMessage(){
  message = 'This is the updated message';
  alert('Listen '+message);
}
  
alert(message);
displayMessage();
alert(message);
</script>

The outer variable remains unchanged before we invoke the function but after calling the function the value changes and hence even the last alert function prints the changed value to the screen.

Passing parameter
We can even arbitrary data to functions using parameters.
Example:




<script>
// passing data using parameters
function sayHello(from, via){
  alert('You have a message from ' + from + ' via '+ via);
}
sayHello('Mom', 'WhatsApp');
sayHello('Dad', 'Telegram');
</script>

In the above we simply passed two parameters, and used them inside the main function. If somehow, one or all the parameters are not provided then javascript uses default parameters. In that case it basically prints ‘undefined’ in place of the expected output.
Example:




<script>
// no parameter assigned
function sayHello(from, via){
alert('You have a message from ' + from + ' via '+ via);
}
sayHello('Mom'); // default 2 parameter
</script>

We can also make use of deault parameter. We simply assign the value in the arugment body itself like:
Example




<script>
// using default parameter
function sayHello(from, via='not mentioned'){
alert('You have a message from ' + from + ' via '+ via);
}
sayHello('Mom');
</script>

Returning a value
A function can return a value back to the code which is calling it.
Example:




<script>
// simple function
function sum(a, b) {
  return a + b;
}
  
let result = sum(5, 10);
alert( result ); // 15
</script>

In the above code, we simply create a function and passed two values in the function arguments and then stores the value in a variable result and finally alerts it. This is generally the most used scenario in which functions are used.
Next topic: JavaScript Course | Objects in JavaScript




My Personal Notes arrow_drop_up
Recommended Articles
Page :