Most Important Functions in JavaScript with Examples

Hi developers,In this article, I’ll introduce you to some of the most important functions in JavaScript along with examples. Understanding these functions is crucial for mastering JavaScript and building dynamic web applications. Let’s dive into each function and explore how they work.

1. Console.log()

This function is used to log messages to the console, which is helpful for debugging and displaying information during development.

Example:

console.log(“Hello World”) 
Output:-Hello World

2. Math.max() Function
  • This function helps you find the highest (maximum) value among a set of numbers.
  • Imagine you have a group of friends standing in a line, and you want to find out who is the tallest among them.
  • Similarly, Math.max() looks at a list of numbers and tells you which one is the biggest.

Example:

const highestNumber = Math.max(5, 10, 8, 3);
console.log(highestNumber); // Output: 10

3. Math.min() Function
  • This function helps you find the lowest (minimum) value among a set of numbers.
  • Suppose you have a basket of apples, and you want to know which apple is the least ripe.
  • Likewise, Math.min() looks at a list of numbers and tells you which one is the smallest.

Example:

const MinimumNumber = Math.min(5, 10, 8, 3);
console.log(MinimumNumber); // Output: 3

4. Math.floor() Function
  • This is a function in JavaScript that rounds a number down to the nearest whole number, or the largest integer less than or equal to a given number.
  • For example:-
    • If you measure something that’s 7.8 inches long, you would round down to 7 inches.
    • If you measure something that’s 9.9 inches long, you would round down to 9 inches.

Example:

const number1 = 7.8;
const number2 = 9.9;
const roundedNumber1 = Math.floor(number1); // Output: 7
const roundedNumber2 = Math.floor(number2); // Output: 9
console.log(roundedNumber1);
console.log(roundedNumber2);

5. document.write() Function

This is a method in JavaScript that allows you to write content directly into an HTML document. It’s commonly used for testing or generating dynamic content.

Syntax:
document.write(” Hello World”)

6. remove() Function

function is used to remove an element from the DOM (Document Object Model) in JavaScript. It’s a method that can be called on a DOM element, and it removes that element from its parent.

Example:
element.remove();
const element = document.getElementById(‘elementToRemove’);

7. alert() Function

The alert() function in JavaScript is used to display a dialog box with a specified message and an OK button.

Syntax:
alert(“Hello world”)

8. setInterval() Function

This function repeatedly calls a function or executes a code snippet at specified intervals (in milliseconds).

Example:
setInterval(function() {
  console.log(“This message will be logged every 2 seconds.”);
}, 2000);

In this example, the setInterval() function calls the provided function (in this case, an anonymous function that logs a message to the console) every 2000 milliseconds (2 seconds).

9. setTimeout() Function

This function executes a function or code snippet once after a specified delay (in milliseconds).

Example:
setTimeout(function() {
  console.log(“This message will be logged after 5 seconds.”);
}, 5000);

In this example, the setTimeout() function calls the provided function (again, an anonymous function that logs a message to the console) after a delay of 5000 milliseconds (5 seconds).

10. callback() Function

In JavaScript, a callback function is a function that you pass as an argument to another function. The function that receives the callback will execute it at some point in the future, usually after it’s done with its own work.

Example:
function greeting(name) { console.log(“Hello, “ + name + “!”); }
function sayHello(callback) {
console.log(“Preparing to say hello…”);  
callback(“John”);
 } 


// Call the function with the callback 
sayHello(greeting);

Leave a Comment