TGViewer
Web Development - HTML, CSS & JavaScript Web Development - HTML, CSS & JavaScript @javascript_courses ยท 55.7K subscribers
Post #3459 1.99K
๐Ÿš€ JavaScript Functions โ€” Complete Beginner to Advanced Guide ๐Ÿ‘จโ€๐Ÿ’ป

Functions -> one of the most important concepts in JavaScript.

A function -> a reusable block of code designed to perform a specific task.

Why Use Functions?

โ€ข โœ… Reuse code

โ€ข โœ… Reduce duplication

โ€ข โœ… Improve readability

โ€ข โœ… Make programs modular

โ€ข โœ… Easier maintenance

๐Ÿง  1. What is a Function?

Instead of writing the same code multiple times, we -> place it inside a function.

Example:

function greet() {
console.log("Hello World");
}


Calling the Function:

โ€ข greet();

Output:

โ€ข Hello World

โšก 2. Function Declaration

The most common way -> create a function.

Syntax:

function functionName() {

}


Example:

function welcome() {
console.log("Welcome to JavaScript");
}


โ€ข welcome();

๐Ÿ”ฅ 3. Function Parameters

Parameters -> variables that receive values.

Example:

function greet(name) {
console.log("Hello " + name);
}


โ€ข greet("Deepak");

Output:

โ€ข Hello Deepak

๐ŸŽฏ 4. Multiple Parameters

function add(a, b) {
console.log(a + b);
}


โ€ข add(10, 20);

Output:

โ€ข 30

๐Ÿ”„ 5. Return Statement

return -> sends a value back from a function.

Example:

function add(a, b) {
return a + b;
}


let result = add(10, 20);

console.log(result);

Output:

โ€ข 30

๐Ÿงฉ 6. Function Expression

A function -> stored inside a variable.

Example:

const greet = function() {
console.log("Hello");
};


โ€ข greet();

Difference:

Concept -> Function Declaration vs Function Expression

โ€ข Hoisted -> Yes vs Not Hoisted

โ€ข Defined -> separately vs Stored in variable

โšก 7. Arrow Functions (ES6)

Modern way -> write functions.

Traditional Function:

function add(a, b) {
return a + b;
}


Arrow Function:

const add = (a, b) => {
return a + b;
};


Short Form:

const add = (a, b) => a + b;


๐Ÿ“ฆ 8. Default Parameters

Default values -> used when arguments are not passed.

Example:

function greet(name = "Guest") {
console.log(name);
}


โ€ข greet();

Output:

โ€ข Guest

๐Ÿง  9. Rest Parameters

Rest parameters -> collect multiple arguments into an array.

Example:

function sum(...numbers) {
console.log(numbers);
}


โ€ข sum(1, 2, 3, 4);

Output:

โ€ข [1, 2, 3, 4]

๐Ÿš€ 10. Scope in JavaScript

Scope -> determines where variables can be accessed.

Global Scope

let name = "Deepak";

function show() {
console.log(name);
}


โ€ข Accessible -> everywhere

Local Scope

function show() {
let age = 25;
console.log(age);
}


โ€ข Accessible -> only inside function

๐Ÿ”’ 11. Block Scope

Variables -> declared using let and const.

if(true) {
let age = 25;
console.log(age);
}


Outside block:

console.log(age);

โŒ Error

๐Ÿง  12. What is Hoisting?

Hoisting -> JavaScript moves declarations to the top before execution.

Example:

sayHello();

function sayHello() {
console.log("Hello");
}


โ€ข Works -> because function declarations are hoisted

๐Ÿ”ฅ 13. Callback Functions

Callback -> function passed as an argument to another function.

Example:
  • ๐Ÿ‘ 4
  • โค 2
More from @javascript_courses
  1. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  2. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  3. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  4. Oct 4, 2026DSA Handwritten Notes
  5. Sep 28, 2026To learn web design and development from basic to advanced levels, you can follow these stโ€ฆ
  6. Sep 27, 202610 Websites Every Developer & AI Enthusiast Should Bookmark โœ… roadmap.sh โ€“ Step-by-step leโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’