Example
function addNumbers(a, b) {
return a + b;
}
console.log(addNumbers(5, 10));

You can also try this code with Online Javascript Compiler
Run Code
Output:
15
In this example, addNumbers takes two parameters (a and b), adds them, and returns the result.
Number Methods
JavaScript provides several methods to handle numbers effectively. Here are a few important ones:
1. toFixed()
Rounds a number to a specified number of decimal places.
let num = 5.6789;
console.log(num.toFixed(2));

You can also try this code with Online Javascript Compiler
Run Code
Output:
5.68
2. parseInt() and parseFloat()
Converts strings to integers and floating-point numbers.
console.log(parseInt("42"));
console.log(parseFloat("3.14"));

You can also try this code with Online Javascript Compiler
Run Code
Output:
42
3.14
Boolean Methods
Booleans in JavaScript are either true or false. Commonly used functions include:
1. Boolean()
Converts a value to its boolean equivalent.
console.log(Boolean(0));
console.log(Boolean(123));

You can also try this code with Online Javascript Compiler
Run Code
Output:
false
true
2. Logical Comparisons
These include &&, ||, and !.
console.log(!false);
console.log(true && false);

You can also try this code with Online Javascript Compiler
Run Code
Output:
true
false
String Methods
String methods help manipulate text efficiently. Here are some essential ones:
1. charAt()
Returns the character at a specific position.
let str = "JavaScript";
console.log(str.charAt(4));

You can also try this code with Online Javascript Compiler
Run Code
Output:
S
2. toUpperCase() and toLowerCase()
Converts a string to uppercase or lowercase.
console.log(str.toUpperCase());
console.log(str.toLowerCase());

You can also try this code with Online Javascript Compiler
Run Code
Output:
JAVASCRIPT
javascript
3. slice()
Extracts a portion of a string.
console.log(str.slice(0, 4));
Output:
Java
String HTML Wrappers
These methods generate HTML-like representations of strings.
1. bold()
Wraps a string in <b> tags.
let text = "Hello";
console.log(text.bold());

You can also try this code with Online Javascript Compiler
Run Code
Output:
<b>Hello</b>
2. fontcolor()
Sets the color of the text.
console.log(text.fontcolor("blue"));

You can also try this code with Online Javascript Compiler
Run Code
Output:
<font color="blue">Hello</font>
Array Methods
Arrays are a fundamental part of JavaScript. Here are some commonly used methods:
1. push() and pop()
Adds and removes elements from the end of an array.
let arr = [1, 2, 3];
arr.push(4);
console.log(arr);
arr.pop();
console.log(arr);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[1, 2, 3, 4]
[1, 2, 3]
2. map()
Applies a function to each element in the array.
let nums = [1, 2, 3];
let squares = nums.map(x => x * x);
console.log(squares);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[1, 4, 9]
Date Methods
JavaScript's Date object helps handle dates and times.
1. getDate() and getFullYear()
Fetch the day and year from a date.
let today = new Date();
console.log(today.getDate());
console.log(today.getFullYear());

You can also try this code with Online Javascript Compiler
Run Code
Output:
(current year)
(current year)
2. setDate()
Sets the day for a date object.
today.setDate(15);
console.log(today);

You can also try this code with Online Javascript Compiler
Run Code
Output:
Updated date with day set to 15
Date Static Methods
Static methods allow you to work directly with the Date class.
1. Date.now()
Returns the current timestamp in milliseconds.
console.log(Date.now());

You can also try this code with Online Javascript Compiler
Run Code
Output:
Current timestamp
2. Date.parse()
Parses a date string into milliseconds.
let ms = Date.parse("2025-01-01");
console.log(ms);

You can also try this code with Online Javascript Compiler
Run Code
Output:
Milliseconds since January 1, 1970
Math Methods
The Math object includes functions for mathematical calculations.
1. Math.round()
Rounds a number to the nearest integer.
console.log(Math.round(4.6));

You can also try this code with Online Javascript Compiler
Run Code
Output:
5
2. Math.random()
Generates a random number between 0 and 1.
console.log(Math.random());

You can also try this code with Online Javascript Compiler
Run Code
Output:
Random number
RegExp Methods
Regular expressions help search and manipulate strings.
1. test()
Checks if a pattern exists in a string.
let regex = /hello/;
console.log(regex.test("hello world"));

You can also try this code with Online Javascript Compiler
Run Code
Output:
true
2. exec()
Returns the first match for a pattern.
console.log(regex.exec("hello world"));

You can also try this code with Online Javascript Compiler
Run Code
Output:
["hello"]
Function Invocation
Function invocation is the process of calling or executing a function. In JavaScript, functions are blocks of code designed to perform a specific task. To use a function, you need to invoke it. This is done by using the function name followed by parentheses `()`.
For example, let’s say we have a function called `greet`:
function greet() {
console.log("Hello, World!");
}
To invoke this function, you simply write:
greet();
When this code runs, it will print "Hello, World!" to the console.
Functions can also take inputs, called parameters. For example:
function greet(name) {
console.log("Hello, " + name + "!");
}
Here, `name` is a parameter. When you invoke the function, you pass an argument to it:
greet("Alice");
This will print "Hello, Alice!" to the console.
Function invocation is a fundamental concept in JavaScript. It allows you to execute code whenever you need it, making your programs more modular & organized.
Function Return
Functions in JavaScript not only perform tasks but can also return values. The `return` statement is used to send a value back to the place where the function was called. This is useful when you want to use the result of a function elsewhere in your code.
Let’s take a simple example. Suppose you want to create a function that adds two numbers & returns the result:
function add(a, b) {
return a + b;
}
Here, the `return` statement sends the sum of `a` & `b` back to the caller. To use this function, you can invoke it & store the result in a variable:
let result = add(5, 3);
console.log(result);

You can also try this code with Online Javascript Compiler
Run CodeOutput:
8
In this example, the function `add` is called with arguments `5` & `3`. It calculates the sum & returns `8`, which is then stored in the `result` variable.
Why is `return` important?
1. Reusability: You can use the returned value in other parts of your code.
2. Modularity: Functions can be designed to perform specific tasks & return results, making your code cleaner & easier to manage.
3. Control Flow: The `return` statement stops the execution of the function. Any code after `return` in the function will not run.
For example:
function checkAge(age) {
if (age >= 18) {
return "You are an adult.";
} else {
return "You are a minor.";
}
console.log("This will not run."); // This line is never executed
}
let message = checkAge(20);
console.log(message);

You can also try this code with Online Javascript Compiler
Run Code
Output:
"You are an adult."
In this case, the function `checkAge` returns a message based on the input `age`. The `console.log` statement after `return` is ignored because the function exits as soon as it hits the `return` statement.
Returning Multiple Values
JavaScript functions can return only one value directly. However, you can return multiple values using an array or an object. For example:
function getUser() {
let name = "Alice";
let age = 25;
return { name: name, age: age }; // Returning an object
}
let user = getUser();
console.log(user.name); // Output: Alice
console.log(user.age);

You can also try this code with Online Javascript Compiler
Run Code
Output:
25
Here, the function `getUser` returns an object containing multiple values.
The () Operator
The `()` operator in JavaScript is used to invoke or call a function. It is one of the most fundamental & frequently used operators in the language. When you see `()` after a function name, it means the function is being executed.
Let’s understand this with an example. Suppose you have a function called `sayHello`:
function sayHello() {
console.log("Hello!");
}
To execute this function, you use the `()` operator:
sayHello(); // Output: Hello!
Without the `()`, the function will not run. For example:
sayHello; // This does nothing. It just refers to the function without calling it.
Using `()` with Parameters
The `()` operator is also used to pass arguments to a function. For instance:
function greet(name) {
console.log("Hello, " + name + "!");
}
greet("Alice");
greet("Bob");

You can also try this code with Online Javascript Compiler
Run Code
Output:
Hello, Alice!
Hello, Bob!
Here, the `()` operator is used to pass the argument `"Alice"` or `"Bob"` to the `greet` function.
The Role of `()` in Function Expressions
In JavaScript, functions can also be defined as expressions. For example:
let multiply = function(a, b) {
return a b;
};
To call this function, you still use the `()` operator:
let result = multiply(4, 5);
console.log(result);

You can also try this code with Online Javascript Compiler
Run Code
Output:
20
Immediately Invoked Function Expressions (IIFE)
Sometimes, you might want to define & execute a function at the same time. This is called an Immediately Invoked Function Expression (IIFE). Let’s see how it works:
(function() {
console.log("This function runs immediately!");
})();
In this example, the function is defined & invoked right away using the `()` operator. This pattern is useful for creating a private scope & avoiding variable conflicts.
Using `()` with Built-in Functions
JavaScript provides many built-in functions, & the `()` operator is used to call them. For example:
let number = "42";
let parsedNumber = parseInt(number); // Converts string to integer
console.log(parsedNumber);

You can also try this code with Online Javascript Compiler
Run Code
Output:
42
Here, `parseInt` is a built-in function, & the `()` operator is used to call it with the argument `"42"`.
Common Mistakes with `()`
1. Forgetting `()`: If you forget to use `()`, the function will not execute.
sayHello; // Does nothing
sayHello(); // Correctly calls the function
2 Using `()` with non-functions: If you try to use `()` on something that is not a function, you’ll get an error.
let x = 10;
x(); // Error: x is not a function
Frequently Asked Questions
What are JavaScript built-in functions?
JavaScript built-in functions are pre-defined methods that perform common tasks like string manipulation, date handling, and mathematical calculations.
Can I use these functions directly in my code?
Yes, these functions are readily available in JavaScript and require no additional libraries.
What is the importance of built-in functions in JavaScript?
They save time and reduce errors by providing optimized solutions for common programming tasks.
Conclusion
In this article, we discussed various built-in functions in JavaScript, including methods for numbers, booleans, strings, dates, and more. These functions simplify coding and make JavaScript a versatile tool for developers.
You can also check out our other blogs on Code360.