Web Development - HTML, CSS & JavaScript
55.6K subscribers
1.92K photos
5 videos
34 files
536 links
Learn to code and become a Web Developer with HTML, CSS, JavaScript , Reactjs, Wordpress, PHP, Mern & Nodejs knowledge

Managed by: @love_data
Download Telegram
let value = "100";

let result = Number(value);


Output: 100

Number to String

let age = 25;

let result = String(age);


Output: 25 (as a string)

String to Boolean

let value = "Hello";
console.log(Boolean(value));


Output: true

8. Variable Naming Rules

Follow these rules when naming variables:

- Names can contain letters, numbers, _ and $.
- Names cannot start with a number.
- Variable names are case-sensitive.
- Reserved keywords cannot be used as variable names.
- Use meaningful names.

Good examples:

let firstName = "Rahul";
let totalPrice = 500;
let isLoggedIn = true;

Invalid examples:

let 1name = "Rahul";
let let = 100;

Double Tap โค๏ธ For Part-3
โค8
๐—š๐—ข๐—ข๐—š๐—Ÿ๐—˜ ๐Ÿฑ ๐—™๐—ฅ๐—˜๐—˜ ๐—ง๐—˜๐—–๐—› ๐—–๐—ข๐—จ๐—ฅ๐—ฆ๐—˜๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ! ๐ŸŽ“๐Ÿ’ป

Explore 5 FREE Google learning courses to build in-demand technical skills and strengthen your resume! ๐Ÿš€

โœ… FREE Learning Resources
โœ… Beginner-Friendly Courses
โœ… Learn at Your Own Pace
โœ… Explore Hands-On Learning Opportunities
โœ… Build Skills for AI, ML & Cloud Careers

๐Ÿ”— ๐—Ÿ๐—ถ๐—ป๐—ธ :- ๐Ÿ‘‡

https://pdlink.in/4kMcA5X

โšก Don't wait to upgrade your skills โ€” start learning today!
โค1
Double Tap โค๏ธ For Part-4
โค6
๐Ÿš€ ๐— ๐—œ๐—–๐—ฅ๐—ข๐—ฆ๐—ข๐—™๐—ง + ๐—Ÿ๐—œ๐—ก๐—ž๐—˜๐——๐—œ๐—ก ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ข๐—จ๐—ฅ๐—ฆ๐—˜๐—ฆ! ๐ŸŽ“๐Ÿ”ฅ

๐Ÿ˜ Explore 5 FREE Career Learning Paths to boost your resume!

๐Ÿ“Š Data Analytics | โ˜๏ธ Cloud Computing | ๐Ÿค– AI | ๐Ÿ” Cybersecurity | ๐Ÿ’ป Software Development

โœ… Learn In-Demand Skills
โœ… Explore Certificate Opportunities
โœ… Perfect for Students, Freshers & Job Seekers

๐Ÿ”— ๐—Ÿ๐—ถ๐—ป๐—ธ :- ๐Ÿ‘‡

https://pdlink.in/4eOufX9

๐Ÿ“ฒ Share with your friends! ๐Ÿ”ฅ
โค3
๐Ÿ’ป JavaScript Learning Series - Day 4

Topic 4: Conditional Statements in JavaScript

Conditional statements allow a program to make decisions based on whether a condition is true or false.

For example:

โ€ข Check whether a person is eligible to vote.
โ€ข Check whether a student has passed or failed.
โ€ข Verify login credentials.
โ€ข Apply discounts based on purchase amounts.

1. The if Statement

The
if

statement executes a block of code only when a condition is true.

Syntax:
if (condition) {
    // Code executes if condition is true
}

Example:
let age = 20;

if (age >= 18) {
    console.log("You are eligible to vote");
}

Output: You are eligible to vote

2. The if-else Statement

Executes one block when the condition is true and another when it is false.
let age = 16;

if (age >= 18) {
    console.log("Eligible to vote");
} else {
    console.log("Not eligible to vote");
}

Output: Not eligible to vote

3. The else-if Statement

Used when you need to check multiple conditions.

Example: Student Grading System
let marks = 85;

if (marks >= 90) {
    console.log("Grade A");
} else if (marks >= 75) {
    console.log("Grade B");
} else if (marks >= 60) {
    console.log("Grade C");
} else if (marks >= 40) {
    console.log("Grade D");
} else {
    console.log("Fail");
}

Output: Grade B

4. Nested if Statements

An
if

statement inside another
if

statement is called a nested
if

statement.

Example: Login Verification
let username = "admin";
let password = "12345";

if (username === "admin") {
    if (password === "12345") {
        console.log("Login Successful");
    } else {
        console.log("Incorrect Password");
    }
} else {
    console.log("Invalid Username");
}

Output: Login Successful

5. The switch Statement

Used when you want to compare one expression against multiple possible values.
switch (expression) {
    case value1:
        // Code
        break;

    case value2:
        // Code
        break;

    default:
        // Default code
}

Example: Day of the Week
let day = 3;

switch (day) {
    case 1:
        console.log("Monday");
        break;

    case 2:
        console.log("Tuesday");
        break;

    case 3:
        console.log("Wednesday");
        break;

    default:
        console.log("Invalid Day");
}

Output: Wednesday

Remember:

โ€ข
case

defines possible matching values.
โ€ข
break

prevents execution from falling through to the next case.
โ€ข
default

executes when no case matches.

6. Ternary Operator

A shorter way to write a simple if-else condition.
condition ? expression1 : expression2;

let age = 21;

let result = age >= 18
    ? "Adult"
    : "Minor";

console.log(result);

Output:
Adult

7. Multiple Conditions Using Logical Operators

AND (&&)

Both conditions must be true.
let age = 25;
let hasLicense = true;

if (age >= 18 && hasLicense) {
    console.log("You can drive");
}

OR (||)

At least one condition must be true.
let isWeekend = false;
let isHoliday = true;

if (isWeekend || isHoliday) {
    console.log("It's a day off");
}

NOT (!)

Reverses a Boolean condition.
let isLoggedIn = false;

if (!isLoggedIn) {
    console.log("Please log in");
}

8. Real-World Example: Online Shopping Discount
let purchaseAmount = 5000;
let discount = 0;

if (purchaseAmount >= 5000) {
    discount = 20;
} else if (purchaseAmount >= 3000) {
    discount = 10;
} else if (purchaseAmount >= 1000) {
    discount = 5;
}

let finalAmount =
    purchaseAmount - (purchaseAmount * discount / 100);

console.log("Purchase:", purchaseAmount);
console.log("Discount:", discount + "%");
console.log("Final Amount:", finalAmount);

Output:
Purchase: 5000
Discount: 20%
Final Amount: 4000
โค4
9. Types of Conditional Statements

๐Ÿ”น if โžœ Checks one condition.

๐Ÿ”น if-else โžœ Chooses between two possible paths.

๐Ÿ”น
else-if

โžœ Checks multiple conditions.

๐Ÿ”น Nested if โžœ Checks dependent conditions inside another condition.

๐Ÿ”น switch โžœ Matches an expression against multiple possible values.

Double Tap โค๏ธ For Part-5
โค7
๐ŸŽ“ ๐Ÿฐ ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ฎ๐˜๐—ถ๐—ผ๐—ป๐˜€ ๐—ง๐—ผ ๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—œ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€

Want to build job-ready skills and strengthen your resume? Start learning these in-demand technologies

๐Ÿ“Š ๐——๐—ฎ๐˜๐—ฎ ๐—”๐—ป๐—ฎ๐—น๐˜†๐˜๐—ถ๐—ฐ๐˜€ :- https://pdlink.in/45vk5ph

๐Ÿ’ซ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐ๐—ธ :- https://pdlink.in/3SuUeuD

โ˜๏ธ ๐——๐—ฎ๐˜๐—ฎ ๐—ฆ๐—ฐ๐—ถ๐—ฒ๐—ป๐—ฐ๐—ฒ :- :- https://pdlink.in/48kyZjt

๐Ÿ›ก๏ธ ๐—”๐—œ ๐—˜๐—ป๐—ด๐—ถ๐—ป๐—ฒ๐—ฒ๐—ฟ๐—ถ๐—ป๐—ด :- :- https://pdlink.in/4fWJVID

๐Ÿ” ๐—ฆ๐—ต๐—ฎ๐—ฟ๐—ฒ this with your friends and classmates!
๐Ÿš€๐Ÿ”ฅ๐—š๐—ข๐—ข๐—š๐—Ÿ๐—˜ ๐—–๐—Ÿ๐—ข๐—จ๐—— โ€“ ๐Ÿฐ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ข๐—จ๐—ฅ๐—ฆ๐—˜๐—ฆ! โ˜๏ธ๐Ÿ“Š

Learn *Data Analytics, BigQuery, Data Engineering & Looker* for FREE!

โœ… Beginner-Friendly Learning
โœ… Build In-Demand Tech Skills
โœ… Boost Your Resume & Career Opportunities

๐ŸŽฏ Perfect for Students, Freshers & Job Seekers!

๐Ÿ”— ๐—Ÿ๐—ถ๐—ป๐—ธ :- ๐Ÿ‘‡

https://pdlink.in/4AttkUb

๐Ÿ“ฒ Save & Share with your friends! ๐Ÿ”ฅ
โค1๐Ÿ‘1
๐Ÿ’ป JavaScript Learning Series: Day 5

Topic 5: Loops in JavaScript

Loops execute a block of code repeatedly while a condition is met. They save you from writing the same code over and over.

Without a loop:

console.log("Hello");
console.log("Hello");
console.log("Hello");
console.log("Hello");
console.log("Hello");


Using a loop:

for (let i = 1; i <= 5; i++) {
console.log("Hello");
}


Both produce the same output, but loops make code shorter and easier to maintain.

1. for Loop

A for loop is useful when you know how many times you want to run a block of code.

Syntax:

for (initialization; condition; update) {
// Code to execute
}


The three parts are:

โ€ข Initialization sets the starting value.

โ€ข Condition checks whether the loop should continue.

โ€ข Update changes the variable after each iteration.

Example:

for (let i = 1; i <= 5; i++) {
console.log(i);
}


Output:

1
2
3
4
5


Here, i starts at 1, increases by 1 after each iteration, and the loop ends when i <= 5 is false.

2. while Loop

A while loop runs as long as its condition is true. Itโ€™s useful when you donโ€™t know exactly how many iterations are needed.

while (condition) {
// Code to execute
}


let i = 1;

while (i <= 5) {
console.log(i);
i++;
}


The condition is checked before each iteration. Make sure the loop can eventually end, or it may run forever.

3. do...while Loop

A do...while loop runs its code at least once before checking the condition.

do {
// Code to execute
} while (condition);


do {
console.log(i);
i++;
} while (i <= 5);


Key difference:

โ€ข for is commonly used when the number of iterations is known.

โ€ข while checks the condition before running the code.

โ€ข do...while runs the code once before checking the condition.

4. for...of Loop

Use for...of to iterate over values in iterables such as arrays, strings, maps, and sets.

Example: iterating through an array:

let fruits = ["Apple", "Mango", "Banana"];

for (let fruit of fruits) {
console.log(fruit);
}


Apple
Mango
Banana


Example: iterating through a string:

let name = "JavaScript";

for (let character of name) {
console.log(character);
}


This prints each character separately.

5. for...in Loop

Use for...in to iterate over an object's enumerable property keys.

let student = {
name: "Rahul",
age: 22,
city: "Delhi"
};

for (let key in student) {
console.log(key, student[key]);
}


name Rahul
age 22
city Delhi


key is the property name, and student[key] retrieves its value. For arrays, prefer for...of or array methods.

6. Nested Loops

A nested loop is a loop inside another loop. Itโ€™s commonly used for patterns, matrices, and multidimensional arrays.

for (let i = 1; i <= 3; i++) {
for (let j = 1; j <= 3; j++) {
console.log(i, j);
}
}


1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
โค3
The inner loop completes all its iterations for each iteration of the outer loop.

7. break Statement

break immediately ends the loop.

for (let i = 1; i <= 10; i++) {
if (i === 5) {
break;
}

console.log(i);
}


```text
1
2
3
4


When i becomes 5, the loop stops.

8. continue Statement

continue skips the current iteration and moves to the next one.

javascript
for (let i = 1; i <= 5; i++) {
if (i === 3) {
continue;
}

console.log(i);
}


text
1
2
4
5


The loop skips 3 but continues running.

9. Real-World Example: Calculate Total Salary

javascript
let salaries = [25000, 30000, 35000, 40000];

let total = 0;

for (let salary of salaries) {
total += salary;
}

console.log("Total Salary:", total);


text
Total Salary: 130000


The loop visits each salary and adds it to the total.

10. Common Loop Patterns

A. Print even numbers

javascript
for (let i = 2; i <= 10; i += 2) {
console.log(i);
}


Output: 2, 4, 6, 8, 10

B. Print numbers in reverse

javascript
for (let i = 10; i >= 1; i--) {
console.log(i);
}


C. Calculate the sum of numbers

javascript
let sum = 0;

for (let i = 1; i <= 10; i++) {
sum += i;
}

console.log(sum);


text
55


D. Print a multiplication table

javascript
let number = 5;

for (let i = 1; i <= 10; i++) {
console.log(number + " x " + i + " = " + (number * i));
}


text
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
...
5 x 10 = 50`


11. Common Mistakes to Avoid

โ€ข Forgetting to update the loop variable.

โ€ข Using the wrong loop condition.

โ€ข Accidentally creating an infinite loop.

โ€ข Confusing break with continue.

โ€ข Using for...in unnecessarily for arrays.

โ€ข Making nested loops more complicated than necessary.

โ€ข Using <= instead of < when it causes an extra iteration.

12. Quick Revision: Types of Loops

๐Ÿ”น for loop โ†’ Best when the number of iterations is known.

๐Ÿ”น while loop โ†’ Repeats while a condition remains true.

๐Ÿ”น do...while loop โ†’ Runs at least once before checking the condition.

๐Ÿ”น for...of loop โ†’ Iterates over values in iterable objects.

๐Ÿ”น for...in loop โ†’ Iterates over enumerable property keys.

๐Ÿ”น Nested loop โ†’ A loop inside another loop.

๐Ÿ”น break โ†’ Terminates the loop.

๐Ÿ”น continue โ†’ Skips the current iteration.

Key Takeaways

โ€ข Loops automate repetitive tasks.

โ€ข Know when to use for, while, and do...while.

โ€ข Use for...of for iterable values and for...in for object property keys.

โ€ข Use break to stop a loop and continue to skip an iteration.

โ€ข Practice nested loops and common coding patterns.

Double Tap โค๏ธ For More
โค1
๐Ÿฏ ๐—™๐—ฅ๐—˜๐—˜ ๐—›๐˜‚๐—ฏ๐—ฆ๐—ฝ๐—ผ๐˜ ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ถ๐—ผ๐—ป๐˜€ ๐—ณ๐—ผ๐—ฟ ๐—ฆ๐˜๐˜‚๐—ฑ๐—ฒ๐—ป๐˜๐˜€ & ๐—™๐—ฟ๐—ฒ๐˜€๐—ต๐—ฒ๐—ฟ๐˜€ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐ŸŽ“

๐Ÿ’ซHubSpot Academy offers three free online certifications that match these interests.

๐Ÿ’ซPick one free course and try it out on your next college event and the certification is free.

๐Ÿ”— ๐—Ÿ๐—ถ๐—ป๐—ธ :- ๐Ÿ‘‡

https://pdlink.in/4iM5xaQ

๐Ÿ“ฒ *Save & Share with your friends! ๐Ÿ”ฅ*
โค2
๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—š๐—ฒ๐˜ ๐—ฌ๐—ผ๐˜‚๐—ฟ ๐——๐—ฟ๐—ฒ๐—ฎ๐—บ ๐—๐—ผ๐—ฏ ๐—œ๐—ก ๐—š๐—ผ๐—ผ๐—ด๐—น๐—ฒ๐Ÿ”ฅ

Discover *4 FREE resources* to help you prepare for a career at Google!

โœ… Improve Your Coding Skills
โœ… Strengthen Interview Preparation
โœ… Build Technical Knowledge
โœ… Prepare for Top Tech Opportunities

๐ŸŽ“Perfect for Students, Freshers & Aspiring Software Engineers!

๐Ÿ”— ๐—Ÿ๐—ถ๐—ป๐—ธ ๐Ÿ‘‡:-

https://pdlink.in/4AwRPQy

๐Ÿ“ฒ SAVE & SHARE with friends dreaming of working at Google!
โค1
๐—™๐—ฅ๐—˜๐—˜ ๐—ฉ๐—œ๐—ฅ๐—ง๐—จ๐—”๐—Ÿ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐—ช๐—œ๐—ง๐—› ๐—ง๐—ข๐—ฃ ๐—–๐—ข๐— ๐—ฃ๐—”๐—ก๐—œ๐—˜๐—ฆ! ๐ŸŽ“

๐Ÿ’ป Explore FREE Virtual Internship & Job Simulation Programs from leading companies!

โœ… Learn Industry-Relevant Skills
โœ… Work on Practical Tasks
โœ… Self-Paced Online Learning
โœ… Explore Completion Certificates
โœ… Strengthen Your Resume & LinkedIn Profile

๐ŸŽฏ Perfect for Students, Freshers & Job Seekers

๐Ÿ”— ๐—Ÿ๐—ถ๐—ป๐—ธ ๐Ÿ‘‡:-

https://pdlink.in/4t8DRlq

๐Ÿ“ฒ SAVE & SHARE with friends looking for career opportunities!
๐Ÿฐ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐—ง๐—ผ ๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—˜๐˜…๐—ฐ๐—ฒ๐—น ๐—™๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜ ๐Ÿ“Š

๐ŸŽ“ Explore 4 FREE Excel Courses to upgrade your skills!

โœ… Excel Formulas & Functions
โœ… Pivot Tables & Charts
โœ… Data Cleaning & Analysis
โœ… Dashboards & Reporting

๐ŸŽฏ Perfect for Students, Freshers & Aspiring Data Analysts!

๐Ÿ”— ๐—Ÿ๐—ถ๐—ป๐—ธ ๐Ÿ‘‡:-

https://pdlink.in/48mO4B2

๐Ÿ“Œ SAVE & SHARE with friends who want to master Excel! ๐Ÿ”ฅ
๐Ÿ’ป JavaScript Learning Series - Day 6

Topic 6: Functions in JavaScript

Functions are one of the most important concepts in JavaScript.

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

Instead of writing the same code repeatedly, you can create a function and execute it whenever needed.

For example:

โ€ข Calculate employee salaries.

โ€ข Validate user login details.

โ€ข Calculate shopping discounts.

โ€ข Process customer information.

โ€ข Perform mathematical calculations.

Let's understand functions step by step.

1. What Is a Function?

A function is a block of code that performs a particular task when called.

function greet() {
console.log("Hello, Welcome!");
}

greet();


Output:

Hello, Welcome!


Explanation:

โ€ข function โžœ Keyword used to declare a function.

โ€ข greet โžœ Function name.

โ€ข () โžœ Holds parameters, if required.

โ€ข {} โžœ Contains the function's code.

โ€ข greet() โžœ Calls or executes the function.

Important: Defining a function does not automatically execute it. You must call it.

2. Function Declaration

A function declaration defines a function using the function keyword.

Syntax:

function functionName() {
// Code to execute
}


Example:

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

welcome();


Output:

Welcome to JavaScript


Function declarations can generally be called before their definition because they are hoisted.

3. Functions with Parameters

Parameters allow you to pass information into a function.

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

greet("Rahul");
greet("Priya");


Output:

Hello Rahul
Hello Priya


Here:

โ€ข name is a parameter.

โ€ข "Rahul" and "Priya" are arguments.

Difference:

Parameter โžœ Variable listed in the function definition.

Argument โžœ Actual value passed when calling the function.

4. Functions with Return Values

The return statement sends a value back from a function.

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

let result = add(10, 20);

console.log(result);


Output:

30


โ€ข The function receives two values.

โ€ข It adds them.

โ€ข return sends the result back.

โ€ข The result is stored in the result variable.

Important: return also terminates the function's execution.

5. Function Expression

A function expression stores a function inside a variable.

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

greet();


Output:

Hello JavaScript


Unlike function declarations, function expressions assigned to variables cannot generally be called before their initialization.

6. Arrow Functions

Arrow functions provide a shorter syntax for writing functions.

They were introduced in ES6.

Traditional function:

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


Arrow function:

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

console.log(add(10, 20));


Output:

30


For a single expression, you can write:

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

console.log(add(5, 10));


Output:

15
Arrow functions are commonly used in modern JavaScript, callbacks, and array methods.

Note: Arrow functions do not have their own this binding.

7. Anonymous Functions

An anonymous function is a function without a name.

setTimeout(function() {
console.log("Hello after 2 seconds");
}, 2000);


Output after approximately 2 seconds:

Hello after 2 seconds


Anonymous functions are frequently used as callbacks.

8. Default Parameters

Default parameters provide fallback values when an argument is missing or undefined.

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

greet("Rahul");
greet();


Output:

Hello Rahul
Hello Guest


9. Rest Parameters

Rest parameters allow a function to accept multiple arguments and collect them into an array.

function calculateTotal(...numbers) {
let total = 0;

for (let num of numbers) {
total += num;
}

return total;
}

console.log(calculateTotal(10, 20, 30, 40));


Output:

100


Here, ...numbers collects all the supplied arguments into one array.

10. Callback Functions

A callback is a function passed as an argument to another function.

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

callback();
}

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

greet("Rahul", message);


Output:

Hello Rahul
Welcome to JavaScript


Callbacks are widely used in:

โ€ข Event handling.

โ€ข Asynchronous programming.

โ€ข Array methods.

โ€ข API operations.

11. Immediately Invoked Function Expression (IIFE)

An IIFE is a function that executes immediately after it is defined.

(function() {
console.log("Function executed immediately");
})();


Output:

Function executed immediately


IIFEs were traditionally used to create private scopes and avoid polluting the global namespace.

12. Function Scope

Scope determines where a variable can be accessed.

JavaScript has different types of scope.

A. Global Scope

Variables declared outside functions and blocks may be accessible throughout their containing script or module environment.

let name = "Rahul";

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


B. Function Scope

Variables declared with var inside a function are accessible within that function.

function test() {
var age = 25;
console.log(age);
}

test();


C. Block Scope

Variables declared using let and const inside a block are accessible only within that block.

if (true) {
let age = 25;
const city = "Delhi";
}

console.log(age); // ReferenceError


13. Higher-Order Functions

A higher-order function is a function that accepts another function as an argument or returns a function.

function calculate(a, b, operation) {
return operation(a, b);
}

function add(x, y) {
return x + y;
}

console.log(calculate(10, 20, add));


Higher-order functions are widely used in functional programming and array operations.

14. Real-World Example: Employee Salary Calculator
โค2
function calculateSalary(basicSalary, bonus) {
let totalSalary = basicSalary + bonus;

return totalSalary;
}

function displaySalary(name, salary) {
console.log("Employee:", name);
console.log("Total Salary:", salary);
}

let salary = calculateSalary(50000, 10000);

displaySalary("Rahul", salary);


Output:

Employee: Rahul
Total Salary: 60000


This example demonstrates how functions help organize and reuse business logic.

15. Common Mistakes Beginners Should Avoid

โ€ข Forgetting to call a function after declaring it.

โ€ข Confusing parameters with arguments.

โ€ข Forgetting the return statement.

โ€ข Using variables outside their scope.

โ€ข Confusing arrow functions with regular functions.

โ€ข Passing incorrect arguments.

โ€ข Creating unnecessarily large functions.

โ€ข Forgetting that a function without an explicit return statement returns undefined.

16. Quick Revision: Types of Functions

๐Ÿ”น Function Declaration โžœ Defines a function using the function keyword.

๐Ÿ”น Function Expression โžœ Stores a function inside a variable.

๐Ÿ”น Arrow Function โžœ Provides shorter function syntax.

๐Ÿ”น Anonymous Function โžœ Function without a name.

๐Ÿ”น Callback Function โžœ Function passed as an argument to another function.

๐Ÿ”น IIFE โžœ Function that executes immediately after definition.

๐Ÿ”น Higher-Order Function โžœ Accepts or returns another function.

๐Ÿ”น Recursive Function โžœ A function that calls itself.

Key Takeaways

โ€ข Functions make code reusable and organized.

โ€ข Parameters allow functions to accept input.

โ€ข Return statements send results back.

โ€ข Arrow functions simplify function syntax.

โ€ข Callbacks allow functions to work with other functions.

โ€ข Scope determines variable accessibility.

โ€ข Higher-order functions are important in modern JavaScript.

Double Tap โค๏ธ For More
โค4