Skip to main content

Python type() Function

When learning Python, you will work with different types of data, such as numbers, text, decimal values, and Boolean values. Python provides several built-in functions to help you work with these data types. One of the most useful functions is the type() function . The type() function tells you the data type of a value or variable. It helps you understand what kind of data Python is working with and can make debugging your programs easier. For example, consider these variables: age = 20 price = 99.50 name = "Rahul" is_student = True Although all four variables store data, their data types are different. Python allows us to check these types using type() . Let's understand how it works with simple examples. 1. What Is the type() Function in Python? The type() function is a built-in Python function that returns information about the type of an object. In beginner-level programming, you can use it to check whether a value is an integer, a floating-point number, a string,...

JavaScript Functional Thinking: Higher-Order Functions and Modern Coding Patterns

Modern JavaScript is not just about variables, loops, and functions. Professional developers use a programming style called Functional Programming to write cleaner, more predictable, and maintainable code.

Frameworks and libraries such as React heavily rely on functional programming concepts.

In this article, we will learn:

  • What Higher-Order Functions are

  • Functions as First-Class Citizens

  • Callback Functions

  • Returning Functions

  • Pure Functions

  • Side Effects

  • Immutability

  • Function Composition

  • Modern Coding Patterns

  • Real-World Examples


Functions are First-Class Citizens

In JavaScript, functions are treated like normal values.

They can be:

  • Assigned to variables

  • Passed as arguments

  • Returned from functions

  • Stored in arrays and objects

Example:

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

greet();

Output:

Hello

What is a Higher-Order Function?

A Higher-Order Function is a function that:

Accepts Another Function

OR

Returns Another Function

Example

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

function execute(fn)
{
    fn();
}

execute(greet);

Output:

Hello

Why are Higher-Order Functions Useful?

They help:

  • Reduce code duplication

  • Improve flexibility

  • Increase reusability

  • Simplify data processing


Callback Functions

A callback is a function passed as an argument.

Example:

function process(callback)
{
    callback();
}

process(
function()
{
    console.log(
    "Processing"
    );
}
);

Output:

Processing

Real-Life Example

Imagine ordering food.

Order Food
     ↓
Food Ready
     ↓
Call Customer

The customer notification acts like a callback.


Returning Functions

Functions can return other functions.

Example:

function multiplyBy(x)
{
    return function(y)
    {
        return x * y;
    };
}

Usage

const double =
multiplyBy(2);

console.log(
double(5)
);

Output:

10

Function Factory

The previous example is called a Function Factory.

It creates customized functions.

const triple =
multiplyBy(3);

Output:

triple(5)

↓

15

Understanding Functional Programming

Functional Programming focuses on:

Functions

Data Transformation

Predictability

Instead of changing data repeatedly.


What is a Pure Function?

A Pure Function:

  1. Produces the same output for the same input.

  2. Does not modify external data.


Example of Pure Function

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

Input:

add(2,3);

Always returns:

5

Why is it Pure?

Because:

No External Variables

No Side Effects

Impure Function

let total = 0;

function add(value)
{
    total += value;
}

Why impure?

Because:

Changes External Data

Side Effects

A Side Effect occurs when a function changes something outside itself.

Examples:

  • Modifying global variables

  • Making API calls

  • Writing to database

  • Updating DOM

Example:

let count = 0;

function increment()
{
    count++;
}

This creates a side effect.


Pure vs Impure Functions

Pure FunctionImpure Function
PredictableUnpredictable
No Side EffectsHas Side Effects
Easy TestingHard Testing
ReusableLess Reusable

Immutability

Immutability means:

Do Not Modify Existing Data

Create New Data Instead

Mutable Example

let numbers =
[1,2,3];

numbers.push(4);

Original array changed.


Immutable Example

let numbers =
[1,2,3];

let newNumbers =
[
...numbers,
4
];

Original array remains unchanged.


Why Immutability Matters

Benefits:

  • Easier debugging

  • Better performance

  • Predictable behavior

  • Useful in React


Function Composition

Combining multiple functions together.

Example:

function double(x)
{
    return x * 2;
}

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

Usage:

let result =
square(
double(5)
);

console.log(result);

Output:

100

Breaking It Down

double(5)

↓

10

↓

square(10)

↓

100

Functional Array Methods

Modern JavaScript heavily uses:

  • map()

  • filter()

  • reduce()

These are Higher-Order Functions.


map()

let numbers =
[1,2,3];

let doubled =
numbers.map(
num => num * 2
);

Output:

[2,4,6]

filter()

let numbers =
[1,2,3,4,5];

let even =
numbers.filter(
num =>
num % 2 === 0
);

Output:

[2,4]

reduce()

let numbers =
[1,2,3,4];

let total =
numbers.reduce(
(sum,num)=>
sum+num
);

Output:

10

Chaining Methods

Example:

let result =
[1,2,3,4,5]

.filter(
num =>
num % 2 === 0
)

.map(
num =>
num * 10
);

Output:

[20,40]

Real-World Example

Student marks:

let marks =
[45,60,80,30,90];

Select passed students:

let passed =
marks.filter(
mark =>
mark >= 50
);

Output:

[60,80,90]

Benefits of Functional Programming

  • Cleaner code

  • Easier maintenance

  • Better testing

  • Less bugs

  • Improved readability


Common Interview Questions

What is a Higher-Order Function?

A function that accepts another function or returns another function.


What is a Pure Function?

A function that always returns the same output for the same input and has no side effects.


What is Immutability?

Avoiding modification of existing data and creating new data instead.


Why is map() a Higher-Order Function?

Because it accepts a callback function.


Practical Project – Custom Calculator Factory

function calculator(type)
{
    if(type==="double")
    {
        return function(x)
        {
            return x*2;
        };
    }

    if(type==="square")
    {
        return function(x)
        {
            return x*x;
        };
    }
}

let double =
calculator("double");

console.log(
double(5)
);

Output:

10

Complete Program

function multiplyBy(x)
{
    return function(y)
    {
        return x*y;
    };
}

let double =
multiplyBy(2);

console.log(
double(10)
);

Output:

20

Real-Life Applications

Functional programming is used in:

  • React Applications

  • State Management

  • Data Processing

  • Financial Systems

  • Analytics Platforms

  • Modern JavaScript Frameworks

Many modern JavaScript applications rely heavily on Higher-Order Functions and functional patterns.


Conclusion

Higher-Order Functions and Functional Programming help developers write cleaner, more maintainable, and predictable code.

By understanding callbacks, function factories, pure functions, immutability, and composition, you gain skills used by professional JavaScript developers every day.

Comments

Popular posts from this blog

Understanding Comments in Java: A Beginner’s Guide

When learning Java programming, one of the first concepts you’ll come across is comments . Comments may not affect how a program runs, but they play a crucial role in making code more understandable, readable, and maintainable. Let’s dive deeper into what comments are, why they’re important, and the different types of comments in Java. What are Comments in Java? A comment in Java is a piece of text written within a program that is ignored by the compiler . In other words, comments are notes for humans, not instructions for machines. They help developers explain the purpose of code, describe logic, or temporarily disable certain lines of code without deleting them. Why are Comments Important? 📌 Readability – Comments make code easier to understand for you and others. 📌 Maintenance – When you revisit your code after months, comments remind you what you were doing. 📌 Collaboration – In team projects, comments explain code to fellow developers. 📌 Debugging – You c...

Java Program to Check Voting Eligibility

In this program, we’ll take the user’s age as input and check whether the person is eligible to vote or not. In India, the minimum age required to vote is 18 years . 💻 Program Code // Java Program to Check Voting Eligibility import java.util.Scanner; public class VotingEligibility { public static void main(String[] args) { // Create Scanner object to take input from user Scanner sc = new Scanner(System.in); // Ask user to enter age System.out.print("Enter your age: "); int age = sc.nextInt(); // Check eligibility if (age >= 18) { System.out.println("You are eligible to vote."); } else { System.out.println("You are not eligible to vote yet."); System.out.println("You can vote after " + (18 - age) + " year(s)."); } sc.close(); // Close the scanner } } 🧠 Explanation import java.util.Scanner; → Imp...

Java Program to Calculate Total Marks and Average of Five Subjects

💡 Program Explanation In this program: The user will input marks of five subjects . The program will calculate the total marks and average . It will then display both results clearly. 🧑‍💻 Java Code: // Java Program to Calculate Total Marks and Average of Five Subjects import java.util.Scanner; // Importing Scanner class for user input public class TotalAndAverage { public static void main(String[] args) { // Creating Scanner object to take input from user Scanner sc = new Scanner(System.in); // Declaring variables for five subjects double subject1, subject2, subject3, subject4, subject5; // Taking marks input for each subject System.out.print("Enter marks of Subject 1: "); subject1 = sc.nextDouble(); System.out.print("Enter marks of Subject 2: "); subject2 = sc.nextDouble(); System.out.print("Enter marks of Subject 3: "); subject3 = sc.nextDo...