How to write a function in JavaScript
In this tutorial, we will learn about what a function does, its types, and the main uses of the functions in JavaScript.
What is a function Definition?
It is a statement which starts with the function keywords, function’s name and a list of function arguments contained in parenthesis and separated by commas, and where statements are enclosed in curly brackets.
Syntax:
function name(arguments)
{
javascript statements
}
Function calling:
It is a process where we will simply type the function’s name. And by default, all JavaScript functions can utilize the argument objects.
Function Arguments:
It is a process where it contains one or more arguments which this can be sent by calling the code and can be used within the function. And it is a dynamically typed programming language, where it can have any type of data as a value.
Function Expression:
It is a assigning of a function to a variable and then use that variable as a function in Javascript.
Types of functions
- Named function
- Anonymous function
- Nested function
- Immediately invoked function expression
Named Function: It is a process whenever we want to call the function we need to write the code by using the referencing its name and providing it with some parameters. It will helps to call a function several times to give various values to it.
Anonymous Function: It is a process where we can define a function without giving it a name. This type of function is said to be Anonymous function.
Nested Function: It is a function where it contains one or more inner functions. It has the ability to access the inner function to the variables and arguments of the outer function.
Immediately invoked function expression: It is a process where the expression will be given, it will detect as soon as it browse the engine.
Example function code for Library Management System
function Library() { this.books = []; this.addBook = function (title, author, year) { const book = { title: title, author: author, year: year }; this.books.push(book); console.log(`Book titled "${title}" by ${author} added to the library.`); }; this.removeBook = function (title) { const index = this.books.findIndex(book => book.title === title); if (index !== -1) { const removedBook = this.books.splice(index, 1)[0]; console.log(`Book titled "${removedBook.title}" removed from the library.`); } else { console.log(`Book titled "${title}" not found in the library.`); } }; this.searchByTitle = function (title) { const foundBooks = this.books.filter(book => book.title.includes(title)); if (foundBooks.length > 0) { console.log(`Books found with title containing "${title}":`, foundBooks); } else { console.log(`No books found with title containing "${title}".`); } return foundBooks; }; this.searchByAuthor = function (author) { const foundBooks = this.books.filter(book => book.author.includes(author)); if (foundBooks.length > 0) { console.log(`Books found by author "${author}":`, foundBooks); } else { console.log(`No books found by author "${author}".`); } return foundBooks; }; } const myLibrary = new Library(); myLibrary.addBook('To Kill a Mockingbird', 'Harper Lee', 1960); myLibrary.addBook('1984', 'George Orwell', 1949); myLibrary.addBook('The Great Gatsby', 'F. Scott Fitzgerald', 1925); myLibrary.searchByTitle('1984'); myLibrary.searchByAuthor('George Orwell'); myLibrary.removeBook('1984'); myLibrary.searchByTitle('1984');