By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
Notification Show More
Latest News
Passing a JavaScript Value Between HTML Pages
Passing a JavaScript Value Between HTML Pages
JavaScript
Compare Objects in an Array
JavaScript Problem: Compare Objects in an Array
JavaScript
Switching Name Order Using Capturing Groups in Regular Expressions
Switching Name Order Using Capturing Groups in Regular Expressions
JavaScript
Shuffling an Array
JavaScript Problem: How to Perform Shuffling an Array
JavaScript
Create a Non-duplicated Collection
JavaScript Problem: Using Set to Create a Non-duplicated Collection
JavaScript
Aa
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Functions in JavaScript
JavaScript

Functions in JavaScript

Rohan750
Last updated: 2021/05/16 at 5:36 AM
Rohan750
Share
3 Min Read

Contents
1)Function Definition2)Calling functions3)Function Parameter vs Function Arguments 4)Function expressions5) Return Keyword6) Anonymous Function
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

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Control Statement and loops
Next Article Modern JavaScript
4 Comments 4 Comments
  • Hollis says:
    November 16, 2021 at 11:40 pm

    I always spent my half an hour to read this webpage’s posts
    everyday along with a mug of coffee.

    Reply
  • Gus says:
    November 18, 2021 at 11:42 pm

    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!

    Reply
  • Candra says:
    November 22, 2021 at 5:51 pm

    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.

    Reply
  • Hans says:
    November 23, 2021 at 5:32 pm

    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.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

Passing a JavaScript Value Between HTML Pages

Passing a JavaScript Value Between HTML Pages

February 3, 2023
Compare Objects in an Array

JavaScript Problem: Compare Objects in an Array

January 30, 2023
Switching Name Order Using Capturing Groups in Regular Expressions

Switching Name Order Using Capturing Groups in Regular Expressions

January 29, 2023
Shuffling an Array

JavaScript Problem: How to Perform Shuffling an Array

January 27, 2023
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?