Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Static variable

Hey, guys does anyone have a logical explantation to this:

function f() {
    f.count = ++f.count || 1
}

what extacly makes f.count a static variable, i don't get it

2 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hi asaf,

Are you pulling this code from a specific video/course? I'm a bit confused because f.count is not a variable. It would help to see the context of where this code is coming from.

You can try reading this Wikipedia article on static variables, but it's not super easy to grasp. Again, if you can help me out with some context, I may be able to boil it down a bit.

Cheers,

Greg

Hey Greg , That's the way to define a static variable in javascript, really weird i just don't get the concept behind it.. its taking the enviorment of f and assigns a count into it, but what is hard to grasp is the ++f.count || 1, basically , you can just add console.log into f function, and see that if you invoke the function outside, its will counter the f.count as static var