DEV Community

Cover image for πŸš€ Higher Order Functions in JavaScript: Let's Dive In!

πŸš€ Higher Order Functions in JavaScript: Let's Dive In!

Jagroop Singh on November 19, 2024

JavaScript has a lot of tricks up its sleeve, and higher-order functions (HOFs) are one of the coolest. Whether you're wrangling data, building UIs...
Collapse
 
john12 profile image
john

After reading the whole blog, questions answer is :
1
2
1
2

Is it correct ?

Collapse
 
jagroop2001 profile image
Jagroop Singh

It's absolutely correct!!
Well you understand HOF

Collapse
 
hraifi profile image
sewiko

Nice explanation about Higher Order functions !!

Collapse
 
jagroop2001 profile image
Jagroop Singh

Thanks @hraifi

Collapse
 
aniruddhaadak profile image
ANIRUDDHA ADAK

wow amazing .

Collapse
 
jagroop2001 profile image
Jagroop Singh

thanks @aniruddhadak

Collapse
 
bobbyleex profile image
BobbyleeX

1
2
2
4
Hope am correct 😁🀣

Collapse
 
jagroop2001 profile image
Jagroop Singh

@bobbyleex ,
Not quite! but still half is correctπŸ˜„ Each call to createCounter creates a new independent counter with its own count variable.

Here’s what happens:

  • counter1() increments its own count variable: 1, then 2.
  • counter2() starts fresh and increments its own count: 1, then 2.

Output:

1
2
1
2
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jawad_abbas-090 profile image
jawad abbas

Nice explanation

Collapse
 
jagroop2001 profile image
Jagroop Singh

Thanks @jawad_abbas-090 @jawe

Collapse
 
works profile image
Web

I thinks it's answer is :
0
1
0
1
or
0 0 0 0

Collapse
 
muhammad_usama_db59ea3065 profile image
Muhammad Usama

answer is
1
2
1
2
if i add another log like
console.log(counter2());
then answer will be:
1
2
1
2
3