JavaScript Functions: A Beginner's Guide to Functions(lt.34)
Introduction to functions in js
Functions in JavaScript are blocks of reusable code that perform a specific task. They are one of the fundamental building blocks of JavaScript programming, by using functions we can make the code more readable and easier to understand.
In JavaScript, Functions are first-class citizens. : Functions in JavaScript are first-class citizens, meaning they can be assigned to variables, passed as arguments to other functions, and returned from functions.
Advantages of using functions
- Code Reusability: Functions allow us to define a block of code once and reuse it multiple times throughout your program.
Modularity: While solving a problem we split them into subproblems so we could solve smaller problems to achieve the final result. Here functions can be used to break down complex problems into smaller, more manageable tasks.
Encapsulation: Functions encapsulate logic, allowing you to hide implementation details and expose only necessary interfaces.
Space Utilization: Functions are the optimal way to manage the space which leads to higher performance of the program.
Introduction to the function declaration.
A function declaration is a way to define a function and give it a name.
The basic syntax for function declaration.
function functionName (parameter1, parameter2, ...)
{ // Function body
}
Calling a function.
Calling a function in JavaScript is very simple. Calling a function simply means executing a block of code that has been declared previously. We can directly call a function by writing functionName followed by parentheses ().
function f1(x)
{
console.log(x*x)
}
f1(3)
// if we want to save the value for further use
function add(x,y) // parameter
{
return x+y
}
let a = add(4,6) // arguments
console.log(a)
//console.log(add(7,3))
In case we we only write return ,or we don't write any return statement it will give undefined as output.
Function with a parameter (Single, and Multiple)
Function with parameter.
function normal(a)
{
console.log(a)
}
normal("hello")
normal("learn js")
Function with one parameter and Function with default parameter.
function message(a = "default display")
{
console.log(a)
}
message("hello this is a argument")
message();
Function with two parameters.
function add(x ,y)
{
return (x+y)
}
let ans = add(10,20)
console.log(ans)
// when the value is not given
function add(x=90 ,y=10)
{
return (x+y)
}
let ans = add(10,20)
console.log(ans)
let ans1 = add()
console.log(ans1)
// when only one value is given
function add(x=90 ,y=10)
{
return (x+y)
}
let ans2 = add(100)
console.log(ans2)
Array as an argument.
// // array as an argument
function sumofnumber([a , b])
{
return a+b
}
let n = [50 ,50,100] // it will take only first two term
let ans = sumofnumber(n)
console.log(ans)
// //type two
function sumofnumber1([a , b=80])
{
return a+b
}
let n1 = [50 ]
let ans1 = sumofnumber1(n)
console.log(ans1)
Function with unlimited parameter
In JavaScript, the arguments keyword refers to an object that contains all the arguments passed to a function. We can make use of the “.length” method to know how many parameters are passed. It is similar to array but is not a array.
It is available within all function bodies.
function sums()
{
let sum = 0;
for (let i = 0; i < arguments.length; i++)
sum += arguments[i]
console.log(sum);
// return sum;
}
sums(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// let a = sums(1,2,3,4,5,6,7,8,9,10)
// console.log(a)
Arrow functions:
Arrow functions, also known as "fat arrow" functions.
Arrow functions are a concise way to write anonymous functions in JavaScript. They were introduced in ES6 and provide a more compact syntax compared to traditional function expressions.
const square =(x)=> x*x
console.log(square(4))
// //passing two arguments
const fun =(x,y)=> x+y
let a =fun(2,3)
console.log(a)
// // multiple statements
const sum = (x,y) =>
{
console.log(`multiply of ${x} and ${y} is`)
return x*y
}
console.log(sum(3,4))
// // RETURNING AN OBJECT
const sumanddif =(x,y)=>({sum:x+y,diff:x-y})
console.log(sumanddif(5,4))
Anonymous Functions:
In general, an anonymous function is a function without a function name but can be assigned to a variable. Arrow functions are also anonymous functions with a compact syntax.
syntax: let variable_name = function ()
{
function body }
// calling of the function
variable_name()
let y = function()
{
console.log("hello")
}
y()
let x = function (x1)
{
console.log(x1*x1)
}
x(4)
Immediately invoked function:
They are also called self invoking function.
It is a JavaScript function that gets called immediately. It is a way to execute functions immediately, as soon as they are created.
(function (n)
{
console.log(n*n)
})(3)
// (function () // anonymous function expression
// {
// console.log("hii")
// })()
// (function add() //named function expression
// {
// console.log("hihhh")
// })()
lt.33link: https://hashnode.com/post/cltl6n1m4000009gu0fqi8kwh
Subscribe to my newsletter
Read articles from himanshu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by