Functions in javascript

JavaScript FUNCTION?

bk_bharathikannan
3 min readAug 18, 2022

*A JavaScript function is a block of code.

*A JavaScript function is executed when “something” invokes it.

SYNTAX ….⬇️

A function is defined with the function keyword, followed by a name, followed by parentheses ().

The code to be executed, by the function, is placed inside curly brackets: {}

TRY THIS:function function_name()
{
console.log("hello world!");
}
function_name();
TRY THIS:function greeting()
{
return `Hello ${user} !`;
}
let user = “bk_bharathikannan”;
let string =greeting();
console.log(string);
ORfunction greeting()
{
return "Hello " + user+"!!";
}
let user = "bk_bharathikannan"
let string =greeting();
console.log(string);

Function parameters & Function arguments…⬇️

Function parameters are the names of variables present in the function definition. Function arguments are the real values that are passed to the function and received by them.

TRY THIS:function greeting(user)
{
return "Hello "+ user + "!!";
}
let string =greeting(“bk_bharathikannan”);
console.log(string);

FUNCTION EXPRESSION⬇️

Anonymous Function⬇️

Anonymous Function is a function that does not have any name. Normally we use the function keyword before the function name to define a function in JavaScript.But, whenever we use anonymous functions in JavaScript, we use only the function keyword without the function name.

TRY THIS:let anonymous_function = function(num1 ,num2)
{
return num1 * num2;
}
anonymous_function(5,5);

OR
let anonymous_function = function(num1 ,num2)
{
return num1 * num2;
}
let result =anonymous_function(5,5);
console.log(result);
let anonymous_function = function(num1 ,num2)
{
return num1 * num2;
}
let sum =anonymous_function;
let result =sum(5,5);
console.log(result);

Local — variable(function)⬇️

* They can only be accessed from within the function.


let str = "bk_bharathikannan";
function string()
{
console.log("THE STRING IS " +str);
}
string();

Global — variable(function)⬇️

A variable declared outside a function, Becomes GLOBAL……

function string() 
{
let name ="bk";
}
console.log(name);
string();

ARROW FUNCTION:-⬇️

* Arrow functions were introduced in ES6.

* Arrow functions allow us to write shorter function syntax:

let myFunction = (a, b) => a * b;

let myFunction = (a, b) => a * b;
myFunction(10,10);
ARROW FUNCTION

HOISTING :-⬇️

In JavaScript, a variable can be declared after it has been used is known as Hoisting…..

GIVE A CLAP IF YOU ENJOYED THIS ARTICLE AND FOLLOW FOR MORE. IT MOTIVATES ME A LOT.🤍

𝔻𝕠 𝕪𝕠𝕦 𝕨𝕒𝕟𝕥 𝕡𝕒𝕣𝕥-𝟚 𝕔𝕠𝕞𝕞𝕖𝕟𝕥 𝕓𝕖𝕝𝕠𝕨⬇️

THINK TWICE💭-code once!

THANKS_FOR_READING_!🌟

❤️“Have a Fine_Day”❤️

--

--