Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save chidimo/430378eff15b5e1f0290bc3a8d1ce5a6 to your computer and use it in GitHub Desktop.

Select an option

Save chidimo/430378eff15b5e1f0290bc3a8d1ce5a6 to your computer and use it in GitHub Desktop.
Comparison of Higher Order Functions (HOF) in JS and Python

## JavaScript Code ## this is some JS code ```javascript let salute = (age) => { let showAge = (name) => { console.log(`Welcome ${name}`) console.log(`Your age is ${age}`) } return showAge } s = salute(16) // returns showAge function s("chidi") console.log(s) // returns the showAge function (name) => { console.log("Welcome " + name) console.log("Your age is " + age) } ``` ## Output Welcome chidi Your age is 16 ## Python Code ```python def salute(age): def show_age(name): print("Welcome ", name) print("Your age is ", age) return show_age # Call the function s = salute(16) # returns the show_age function s("chidi") # pass the my name argument to the show_age function ``` ## Output Welcome chidi Your age is 16

JavaScript Code

let salute = (age) => {
    let showAge = (name) => {
        console.log(`Welcome ${name}`)
        console.log(`Your age is ${age}`)
    }
    return showAge
}

s = salute(16) // returns showAge function
s("chidi")

console.log(s) // returns the showAge function
(name) => {
    console.log("Welcome " + name)
    console.log("Your age is " + age)
}

Output

Welcome chidi

Your age is 16

Python Code

def salute(age):
    def show_age(name):
        print("Welcome ", name)
        print("Your age is ", age)
    return show_age

# Call the function
s = salute(16) # returns the show_age function
s("chidi") # pass the my name argument to the show_age function

Output

Welcome chidi

Your age is 16

JavaScript Code

let salute = (age) => {
    let showAge = (name) => {
        console.log(`Welcome ${name}`)
        console.log(`Your age is ${age}`)
    }
    return showAge
}

s = salute(16) // returns showAge function
s("chidi")

console.log(s) // returns the showAge function
(name) => {
    console.log("Welcome " + name)
    console.log("Your age is " + age)
}

Output

Welcome chidi

Your age is 16

Python Code

def salute(age):
    def show_age(name):
        print("Welcome ", name)
        print("Your age is ", age)
    return show_age

# Call the function
s = salute(16) # returns the show_age function
s("chidi") # pass the my name argument to the show_age function

Output

Welcome chidi

Your age is 16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment