Functions in Javascript

Functions in Javascript

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

  • Function Definition - Before we use a function, we need to define it. A function definition (function declaration or function statement) consists of the function keyword, followed by: - The function's name.- 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 {...}.
  • Calling functions - Defining a function does not execute it. A JavaScript function is executed when "something" invokes it (calls it).

// function definition 
function sum(){
  var a = 10, b = 40;
  var total = a+b;
  console.log(total);
}
sum(); // calling function        
Output: 50        

Function Parameter & Function Arguments:

  • Function parameters are the names listed in the function's definition.
  • Function arguments are the real values passed to the function.

// a and b is parameter
function sum(a,b){
var total = a+b;
console.log(total);
}
sum(20,30); // argument is 20 and 30
sum(50,50); // argument is 50 and 50        
Output: 50
        100        

Why to use functions?

A function is a group of reusable code that can be called anywhere in your program. This eliminates the need to write the same code again and again.

Good coding practice follows DRY => Do not repeat yourself so we use functions.

Function expressions

Function expressions simply mean to create a function and put it into the variable.

function sum(a,b){
var total = a+b;
console.log(total);
}
var funExp = sum(5,15);        
Output: 20        

return keyword

When JavaScript reaches a return statement, the function will stop executing. Functions often compute a return value. The return value is "returned" back to the "caller".

function sum(a,b){
  return total = a+b;
}
var funExp = sum(5,25);
console.log('the sum of two no is ' + funExp );        
output: the sum of two no is 30        

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.

var funExp = function(a,b){
  return total = a+b;
}
var sum = funExp(15,15);
var sum1 = funExp(20,15);
console.log(sum > sum1 );        
Output: false        

By delving into the depths of functions in JavaScript, you've embraced the essence of what it means to be a curious and passionate learner. Your eagerness to expand your knowledge and understanding fuels our own enthusiasm for sharing insights and discoveries with you

Happy Testing ??

要查看或添加评论,请登录

Karishma Yadav的更多文章

  • JAVA Singleton Design Pattern

    JAVA Singleton Design Pattern

    Introduction Ensuring that only one class instance exists can be crucial in software development. The Singleton Pattern…

  • JAVA - String Vs StringBuilder Vs StringBuffer

    JAVA - String Vs StringBuilder Vs StringBuffer

    String It is a sequence of characters. String Class objects are immutable, meaning they cannot be changed once created.

  • Java - Marker Interface

    Java - Marker Interface

    Interface with no fields or methods i.e.

  • Selenium 4- Launch Chrome, Firefox and Edge Browser

    Selenium 4- Launch Chrome, Firefox and Edge Browser

    Pre-requisite to run the selenium test script IDE - Eclipse IDE for Java Developers (version- 4.26.

  • Fundamentals of Git and GitHub

    Fundamentals of Git and GitHub

    What is Git? It is a Version Control System that helps us to track code changes. Why Git? Free and Open Source Fast And…

  • Assertions in Taiko

    Assertions in Taiko

    Assertions is a mechanism used to compare the actual output with the expected output while testing the functionality…

    1 条评论
  • Javascript - Miscellaneous

    Javascript - Miscellaneous

    Difference between null and undefined null and undefined are two distinct types that represent different values. By…

    3 条评论
  • Higher-order Functions in JS

    Higher-order Functions in JS

    Higher order functions are functions that take one or more functions as arguments, or returns a function as its result.…

  • Arrow Functions In Javascript

    Arrow Functions In Javascript

    Arrow functions were introduced in the ES6 version. They make the code more structured and readable.

  • Looping Statements

    Looping Statements

    Looping statement is fundamental control structures in programming. Looping statements are used to execute a block of…

社区洞察

其他会员也浏览了