Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the difference between custom and built-in functions in JavaScript?
JavaScript functions are divided into two main categories: custom (user-defined) functions and built-in functions. Understanding the difference helps you leverage JavaScript's capabilities effectively.
Custom Functions (User-Defined)
Custom functions are created by developers to perform specific tasks. You define the logic, parameters, and return values according to your needs.
Syntax
function functionName(parameter1, parameter2) {
// Your custom logic here
return result;
}
Example: Custom Function
<html>
<head>
<title>Custom Function Example</title>
</head>
<body>
<script>
// Custom function to calculate area of rectangle
function calculateArea(length, width) {
return length * width;
}
// Custom function to greet user
function greetUser(name) {
return "Hello, " + name + "!";
}
var area = calculateArea(10, 5);
var greeting = greetUser("John");
document.write("Area: " + area + "<br>");
document.write(greeting);
</script>
</body>
</html>
Area: 50 Hello, John!
Built-in Functions
Built-in functions are pre-defined functions provided by JavaScript. They perform common operations and are ready to use without any custom implementation.
Common Built-in String Functions
| S. No | Method & Description |
|---|---|
| 1 |
charAt() Returns the character at the specified index. |
| 2 |
charCodeAt() Returns a number indicating the Unicode value of the character at the given index. |
| 3 |
concat() Combines the text of two strings and returns a new string. |
| 4 |
indexOf() Returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found. |
Example: Built-in Function
<html>
<head>
<title>Built-in Function Example</title>
</head>
<body>
<script>
var str1 = "Hello ";
var str2 = "World!";
// Using built-in concat() method
var result = str1.concat(str2);
// Using other built-in methods
var char = result.charAt(0);
var index = result.indexOf("World");
document.write("Concatenated: " + result + "<br>");
document.write("First character: " + char + "<br>");
document.write("Index of 'World': " + index);
</script>
</body>
</html>
Concatenated: Hello World! First character: H Index of 'World': 6
Key Differences
| Aspect | Custom Functions | Built-in Functions |
|---|---|---|
| Definition | Created by developers | Pre-defined by JavaScript |
| Purpose | Specific to your application needs | Common operations |
| Availability | After you define them | Always available |
| Examples | calculateArea(), validateEmail() | parseInt(), Math.max(), String.charAt() |
Conclusion
Custom functions give you flexibility to create application-specific logic, while built-in functions provide efficient solutions for common tasks. Use built-in functions when available to save development time and ensure optimized performance.
