TGViewer
Web Development Web Development @webdevcoursefree ยท 79.7K subscribers
Post #3480 3.78K
Now, let's move to the the next topic:

๐Ÿง  Javascript Functions & Scope

โ“ Why functions matter
- Avoid repeating code
- Make logic reusable
- Improve readability
- Easier debugging
Real use cases:
- Form validation
- Calculations
- API handling
- Button click logic

๐Ÿ”ง What is a function
A function is a block of code designed to perform a task.
Think of it as ๐Ÿ‘‰ Input โ†’ Logic โ†’ Output

โœ๏ธ Function Declaration

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


Call the function
greet();

๐Ÿ“ฅ Functions with parameters

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


greetUser("Deepak");
- name is a parameter
- "Deepak" is an argument

๐Ÿ” Function with return value

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

- return sends value back
- Function execution stops after return

โšก Arrow Functions (Modern JS)
Shorter syntax
Commonly used in React and APIs

const multiply = (a, b) => {
return a * b;
};


Single line shortcut
const square = x => x * x;

๐Ÿง  What is Scope
Scope defines where a variable can be accessed.
Types of scope you must know:
- Global scope
- Function scope
- Block scope

๐ŸŒ Global Scope

let city = "Delhi";
function showCity() {
console.log(city);
}

- Accessible everywhere
- Overuse causes bugs

๐Ÿ  Function Scope

function test() {
let msg = "Hello";
console.log(msg);
}

- msg exists only inside function

๐Ÿงฑ Block Scope (let & const)

if (true) {
let age = 25;
}

- age cannot be accessed outside
- let and const are block scoped
โš ๏ธ var ignores block scope (avoid it)

๐Ÿšซ Common Beginner Mistakes
- Forgetting return
- Using global variables everywhere
- Confusing parameters and arguments
- Using var

๐Ÿงช Mini Practice Task
- Create a function to calculate square of a number
- Create a function that checks if a number is positive
- Create an arrow function to add two numbers
- Test variable scope using let inside a block

โœ… Mini Practice Task โ€“ Solution ๐Ÿง 

๐Ÿ”ข 1๏ธโƒฃ Function to calculate square of a number

function square(num) {
return num * num;
}


console.log(square(5));
โœ”๏ธ Output โ†’ 25

โž• 2๏ธโƒฃ Function to check if a number is positive

function isPositive(num) {
if (num > 0) {
return "Positive";
} else {
return "Not Positive";
}
}
javascript
console.log(isPositive(10));
console.log(isPositive(-3));

โœ”๏ธ Output
- Positive
- Not Positive

โšก 3๏ธโƒฃ Arrow function to add two numbers

const add = (a, b) => a + b;
console.log(add(4, 6));
โœ”๏ธ Output โ†’ 10

๐Ÿงฑ 4๏ธโƒฃ Test variable scope using let inside a block

if (true) {
let message = "Hello Scope";
console.log(message);
}
// console.log(message); โŒ Error

โœ”๏ธ message exists only inside the block

๐Ÿง  Key takeaways
- Functions make code reusable
- return sends output
- Arrow functions are concise
- let respects block scope

โžก๏ธ Double Tap โ™ฅ๏ธ For More
  • โค 11
  • ๐Ÿ‘ 1
More from @webdevcoursefree
  1. Oct 14, 2026๐ŸŒ Web Development Roadmap โ€” Part 17 HTML Images โ€” <img> Tag Images make webpages more visโ€ฆ
  2. Oct 13, 2026๐—ง๐—ข๐—ฃ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ข๐—จ๐—ฅ๐—ฆ๐—˜๐—ฆ ๐—ง๐—ข ๐—•๐—ข๐—ข๐—ฆ๐—ง ๐—ฌ๐—ข๐—จ๐—ฅ ๐—ฆ๐—ž๐—œ๐—Ÿ๐—Ÿ๐—ฆ ๐—œ๐—ก ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ! Explorโ€ฆ
  3. Oct 12, 2026For example: Go to Profile โ†’ <a> Submit Form โ†’ <button> Open Article โ†’ <a> Delete Accountโ€ฆ
  4. Oct 12, 2026๐ŸŒ Web Development Roadmap โ€” Part 16 HTML Links โ€” Tag Links are one of the most importantโ€ฆ
  5. Oct 12, 2026๐Ÿš€ ๐—ช๐—ต๐—ถ๐—ฐ๐—ต ๐—ง๐—ฒ๐—ฐ๐—ต ๐—ฆ๐—ธ๐—ถ๐—น๐—น ๐—ช๐—ถ๐—น๐—น ๐—ฌ๐—ผ๐˜‚ ๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ? Want to builโ€ฆ
  6. Oct 9, 2026Post #4079
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 โ†’