JavaScript Function
Welcome to the JavaScript Function page! Here, you'll find essential concepts and building blocks to kickstart your journey in JavaScript programming.
Types of Functions in JavaScript
JavaScript supports several types of functions, each serving different purposes:
- Function Declarations: Standard way to define a function. Example:
function greet() { console.log("Hello!"); } - Function Expressions: Functions assigned to variables. Example:
const greet = function() { console.log("Hello!"); }; - Arrow Functions: Concise syntax for writing functions. Example:
const greet = () => { console.log("Hello!"); }; - Anonymous Functions: Functions without a name, often used as arguments.
Example:
setTimeout(function() { console.log("This runs after 1 second"); }, 1000); - Immediately Invoked Function Expressions (IIFE): Functions that run as soon as they are defined.
Example:
(function() { console.log("IIFE executed!"); })();
Example Function
Simple Addition Function using Simple Function Declaration
Here's a simple example of a function that adds two numbers and logs the result:
function add(a, b) {
return a + b;
}
console.log(add(5, 10)); // Output: 15