Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,019 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