JS Tutorial



JS FUNCTIONS


JavaScript Functions

Functions in JavaScript are blocks of code that can be called to perform a specific task. They help to organize code, reduce redundancy, and make programs easier to maintain.

๐Ÿ”น Types of Functions:
  • Function Declaration
  • Function Expression
  • Arrow Functions

๐Ÿ“Œ Function Declaration

A Function Declaration defines a named function that can be called anywhere in the code.

Syntax

function functionName(parameters) {
  // code block
}

Example: Simple Addition

function add(a, b) {
  return a + b;
}
let result = add(5, 3);
console.log(result); // Output: 8

๐Ÿ“Œ Function Expression

A Function Expression is a function that is defined within an expression, such as a variable assignment.

Syntax

const functionName = function(parameters) {
  // code block
};

Example: Simple Multiplication

const multiply = function(a, b) {
  return a * b;
};
let result = multiply(5, 3);
console.log(result); // Output: 15

๐Ÿ“Œ Arrow Function

An Arrow Function provides a shorter syntax for writing functions and doesn't have its own this.

Syntax

const functionName = (parameters) => {
  // code block
};

Example: Simple Subtraction

const subtract = (a, b) => {
  return a - b;
};
let result = subtract(5, 3);
console.log(result); // Output: 2

๐Ÿงช Try It Yourself




Function output will appear here
๐Ÿ“˜ Summary:
JavaScript functions come in different forms: Function Declarations, Function Expressions, and Arrow Functions. Choose the type that best suits your needs based on the use case.

๐ŸŒŸ Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review