0% found this document useful (0 votes)
6 views3 pages

Functions of JavaScript

JavaScript functions are blocks of code that perform specific tasks and are essential for writing modular and maintainable programs. There are various types of functions including built-in, user-defined, anonymous, and arrow functions, each serving different purposes. Functions enhance code reusability, modularity, and readability while reducing redundancy.

Uploaded by

sowmyakella91
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Functions of JavaScript

JavaScript functions are blocks of code that perform specific tasks and are essential for writing modular and maintainable programs. There are various types of functions including built-in, user-defined, anonymous, and arrow functions, each serving different purposes. Functions enhance code reusability, modularity, and readability while reducing redundancy.

Uploaded by

sowmyakella91
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Explain the Functions of JavaScript

Introduction
In JavaScript, a function is a block of code designed to perform a specific task. Functions are
one of the most important features of JavaScript because they help in writing modular,
reusable, and maintainable programs. A function executes only when it is called or invoked.

Syntax of a Function

function functionName(parameter1, parameter2, ...){


// block of code
return value;
}

Example

function add(a, b){


return a + b;
}

let result = add(5, 10);


[Link]("Sum = " + result);

Output: Sum = 15

Types of JavaScript Functions

1. Built-in Functions
These are predefined functions provided by JavaScript itself. They can be used directly
without any definition.

Function Description Example

alert() Displays a message box alert("Welcome!")

confirm() Shows OK/Cancel box confirm("Are you sure?")

prompt() Takes user input prompt("Enter your name")

parseInt() Converts string to integer parseInt("10")

[Link]() Finds square root [Link](25)


Date() Displays date/time new Date()

2. User-defined Functions
These are created by programmers to perform specific operations as needed.

function greet(name){
alert("Hello " + name);
}
greet("Sowmya");

Output: Hello Sowmya

3. Anonymous Functions
Functions without a name, usually assigned to a variable.

let multiply = function(x, y){


return x * y;
};
[Link](multiply(3, 4)); // Output: 12

4. Arrow Functions (ES6 Feature)


A shorter way to write functions.

const square = (n) => n * n;


[Link](square(5)); // Output: 25

Advantages of Using Functions


 Code Reusability: Write once and use many times.
 Modularity: Code is divided into smaller, logical parts.
 Easy Debugging: Errors are easier to find and fix.
 Improved Readability: Code becomes cleaner and more understandable.
 Reduces Redundancy: Avoids writing the same code multiple times.

Built-in Function Categories


Category Examples

Browser/Window Functions alert(), confirm(), prompt(), setTimeout()

String Functions toUpperCase(), substring(), concat(), trim()


Math Functions [Link](), [Link](), [Link](),
[Link]()

Date Functions getFullYear(), getMonth(), getDate()

Array Functions push(), pop(), sort(), reverse()

Conclusion
Functions in JavaScript are an essential part of programming. They make the code
organized, efficient, and easy to maintain. JavaScript provides both built-in and user-defined
functions to perform a wide variety of operations.

In short: A function in JavaScript is a reusable block of code that performs a specific task. It
helps in creating structured, readable, and efficient programs.

You might also like