Functions in JavaScript
Published by
sanya sanya
Functions in Javascript is a block of code that will get executed specifically when it gets a call.
Moreover, the function acts like a VIP which will only come on invitation other it will never get invoked until gets called.
There are various uses of using the function in javascript. Firstly, we don't have to write the same code again. Hence, it promotes the DRY principle (Don't Repeat Yourself).
The syntax of the function in Javascript is mentioned below -
function function_name(parameter1, parameter2, ..) { //Code to be executed }
The name of the `function` can be named with letters, digits, underscore, and dollar signs. There can be multiple `parameters` can be included in the function.
When Function Invokes?
The function gets invoked in some of the cases mentioned below -
- When a User clicks a button a function gets invoked.
- It gets called in the code itself.
- It is also self-invoked.
function multiply(a,b) { return a*b } let x = multiply(4,4) console.log(x)
//Output: 16
Function Expression
The Function Expression is the same as the Function Declaration. However, the main difference between the two is the function name.
The Function Keyword can be used with the function inside the parameter as mentioned in the code below -
const sqr_area = function(side) { return side * side; } console.log(sqr_area(2,3)); //Output: 6
Arrow Functions
The Arrow Functions is used to write the code more shortly and smartly. Arrow Functions was introduced in EC6.
The code showing the working of the Arrow Functions is mentioned below -
const sqr_area = (num) => { console.log(num*num); } //Call the function sqr_area(5); //Output: 25
##Regular Functions
The regular functions are the simple functions which can be written as -
function multiply(a,b) { return a+b; }

Library
WEB DEVELOPMENT
Basic
HTML - Hyper Text Markup Language
CSS - Cascading Style Sheets
JavaScript
An Introduction to Javascript!
How to Run JavaScript Code
Variables in Javascript
Numbers in JavaScript
JavaScript Operators
Data Types in JavaScript
Conditional Statements
Switch Statements
Loops in Javascript
Arrays in JavaScript
Strings in JavaScript
Objects in JavaScript
Object Methods in JavaScript
Functions in JavaScript
Object Referencing and Copying in JavaScript
' this' keyword
Asynchronous Programming in JavaScript
Callbacks in JavaScript
Promises in JavaScript
Constructor Functions in JavaScript
Async and Await in JavaScript
Type Conversion in Javascript
DOM
Currying in JavaScript
Network Request
Frontend
Backend
Interview Questions
FAANG QUESTIONS
On this page
When Function Invokes?
Function Expression
Arrow Functions

