27.4 C
Gujarat
HomeJavascriptFunctions in JavaScript

Functions in JavaScript

Functions in javascript


A JavaScript function is a block of code designed to perform a particular task.

1)Function Definition

–>Before we use a function, we need to define it.

–>A function definition (also called a function declaration, or function statement) consists of the function keyword, followed by:
The name of the function.
–>A list of parameters to the function, enclosed in parentheses and separated by commas.
–>The JavaScript statements that define the function, enclosed in curly brackets, {…}.
Example:



//Function Definition 
function sum(){
  var a = 10, b = 40;
  var total = a+b;
  console.log(total);
}

2)Calling functions

–>Defining a function does not execute it.
–>A JavaScript function is executed when “something” invokes it (calls it).

Example:


function sum(){
  var a = 10, b = 40;
  var total = a+b;
  console.log(total);
}


sum(); //function call

3)Function Parameter vs Function Arguments

–>Function parameters are the names listed in the function’s definition. 
–>Function arguments are the real values passed to the function.

Example:


function sum(a,b){
  var total = a+b;
  console.log(total);
}

sum();
sum(20,30);
sum(50,50);
sum(5,6)

output:


PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
NaN
50 
100
11 
PS C:UsersThe-BeastDesktopjavascript Blog> 

4)Function expressions

“Function expressions simply means create a function and put it into the variable “
Example:


function sum(a,b){
  var total = a+b;
  console.log(total);
}

var funExp = sum(5,15);

output:


PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
20
PS C:UsersThe-BeastDesktopjavascript Blog> 

5) Return Keyword

When JavaScript reaches a return statement, the function will stop executing.
The return value is “returned” back to the “caller”.

Example:


function sum(a,b){
  return total = a+b;
}

var funExp = sum(5,25);

console.log('the sum of two no is ' + funExp );

output:


PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
the sum of two no is 30
PS C:UsersThe-BeastDesktopjavascript Blog> 

6) Anonymous Function

A function expression is similar to and has the same syntax as a function declaration One can define “named”.function expressions (where the name of the expression might be used in the call stack for example)  or “anonymous” function expressions.

Example:


var funExp = function(a,b){
  return total = a+b;
}

var sum = funExp(15,15);
var sum1 = funExp(20,15);

console.log(sum);
console.log(sum1);

output:


PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
30
35
PS C:UsersThe-BeastDesktopjavascript Blog> 

watch the video:https://youtu.be/KGkiIBTq0y0

RELATED ARTICLES

4 COMMENTS

  1. This is the perfect website for everyone who would like to understand this topic.
    You know so much its almost tough to argue with you (not that
    I really would want to…HaHa). You definitely put a
    fresh spin on a topic that has been discussed for years.
    Wonderful stuff, just excellent!

  2. Your style is really unique in comparison to other folks I have read stuff
    from. Thank you for posting when you’ve got the opportunity,
    Guess I’ll just bookmark this web site.

  3. Thank you for any other great article. The place else may anybody get that type of information in such a perfect manner of writing?
    I’ve a presentation subsequent week, and I am on the
    search for such info.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

15,000FansLike
5,000FollowersFollow
535FollowersFollow
- Advertisment -spot_img

Subscribe to our newsletter

To be updated with all the latest news, offers and special announcements.

Most Popular