JavaScript Functions

JavaScript Functions

A function in JavaScript is a block of code designed to perform a specific task. Instead of writing the same code again and again, you can put it inside a function and reuse it whenever needed.

Functions help make code modular, reusable, and cleaner.


✔ Function Syntax



 

To run (call) a function:



 


✔ Example


 

Output:

Hello, welcome to JavaScript!

✔ Functions with Parameters

Parameters allow you to pass values into a function.


 

Output:

Hello Vipul
Hello John

✔ Functions with Return Value

A function can return a value using return.


 

Output:

15

✔ Function Expressions

Functions can also be stored in a variable.


 


✔ Arrow Functions (ES6)

Arrow functions provide a shorter syntax.


 


✔ Self-Invoking Function

A function that runs automatically without being called:



 


Summary Table

Type of Function Example
Normal Function function myFun() {}
Function with Parameters function add(a,b){}
Return Function return value;
Function Expression const x = function(){}
Arrow Function const x = () => {}
Self Invoking (function(){})();

✔ Why Use Functions?

  • Reuse code (write once, use many times)

  • Makes code clean and organized

  • Helps in modular programming

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *