DEV Community

param-19
param-19

Posted on

2 2

Functions in JS

A function definition is a regular binding where the value of the binding is a function.For example,

const square = function(x) {
return x * x;
};

  • Each function will have both a definition and declaration.
    • Declaration is basically you simply telling JS to make a particular function, and witholding the info on how the function will process your arguments.

function declare();

  • Definition on the other hand, will tell JS exactly what your function will do with the input arguments.

function declare(){
console.log("Hi, I am Param");
}

  • Scope : Each variable will have a scope, usually global or local.
    • Global scope indicates that the variable can be used by any function or block of code in our program.
    • Local scope means that the variable is valid only within our code block, for example, inside a function. No code segment outside our function(in the above example) will be able to access a local variable.
    • Note : Nesting of functions is possible, i.e., you can declare functions within functions, code blocks within other code blocks. This means that we can have a lot of local variables and global variable pairs too.

The arrow notation :

  • Quoting from EloquentJS, "There’s a notation for functions, which looks very different from the others. Instead of the function keyword, it uses an arrow (=>) ."
  • Its syntax goes like :

    const power = (base, exponent) => {
    let result = 1;
    for (let count = 0; count < exponent; count++) {
    result *= base;
    }
    return result;
    };

    • The arrow comes after all the function arguments, and is followed by the normal function body, as in the usual syntax that we have seen above.

Optional arguments

  • A function can be given additional arguments, in the sense you can provide 3 arguments to it, when it has only one argument. This is perfectly fine, and JS will ignore you and do your work.
  • Also, you can make define a function as

const bio = (name, age = 25) =>{ //age is an optional argument
console.log("Name :" + name + " Age :" + age);
}
//call 1
bio("param");
//call 2
bio("param",19);
//output 1 : Name :param Age : 25
//output 2 : Name :param Age : 19

In the above function, both calls are perfectly valid. 
Enter fullscreen mode Exit fullscreen mode




Recursion

As stated above, you can have functions calling other functions, and even itself within the function. This way of calling a function within itself is known as recursion.

For recursion, the function will have 2 main things :
* A base condition, on which it will exit the recursive call
* The call to function itself
* Optional : Any other logic/programming, if required

function power(base, exponent) {
if (exponent == 0) {
return 1;
} else {
return base * power(base, exponent - 1);
}
}
console.log(power(2, 3));
// → 8

Review

I would love your feedback on this guys, and feel free to revert back to me with any recommendations that you have for me. You can also mail me at padb19122000@gmail.com .

Feature flag article image

Create a feature flag in your IDE in 5 minutes with LaunchDarkly’s MCP server ⏰

How to create, evaluate, and modify flags from within your IDE or AI client using natural language with LaunchDarkly's new MCP server. Follow along with this tutorial for step by step instructions.

Read full post

Top comments (0)

Gen AI apps are built with MongoDB Atlas

Gen AI apps are built with MongoDB Atlas

MongoDB Atlas is the developer-friendly database for building, scaling, and running gen AI & LLM apps—no separate vector DB needed. Enjoy native vector search, 115+ regions, and flexible document modeling. Build AI faster, all in one place.

Start Free

👋 Kindness is contagious

Explore this insightful write-up embraced by the inclusive DEV Community. Tech enthusiasts of all skill levels can contribute insights and expand our shared knowledge.

Spreading a simple "thank you" uplifts creators—let them know your thoughts in the discussion below!

At DEV, collaborative learning fuels growth and forges stronger connections. If this piece resonated with you, a brief note of thanks goes a long way.

Okay