# How to Unit Test Private Functions in JavaScript

DevFeed: [How to Unit Test Private Functions in JavaScript](<https://devfeed.tech/articles/how-to-unit-test-private-functions-in-javascript-29506.md>)

Original publisher: [Read original article](<https://philipwalton.com/articles/how-to-unit-test-private-functions-in-javascript/>)

Published: 2013-07-09T06:28:18Z

Content type: tutorial

Language: en

Sources: [Philip Walton](<https://devfeed.tech/sources/philip-walton.md>)

Topics: [JavaScript](<https://devfeed.tech/topics/javascript.md>), [Unit testing](<https://devfeed.tech/topics/unit-testing.md>)

Tags: [browser](<https://devfeed.tech/tags/browser.md>), [function](<https://devfeed.tech/tags/function.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [javascript](<https://devfeed.tech/tags/javascript.md>), [scope](<https://devfeed.tech/tags/scope.md>), [testing](<https://devfeed.tech/tags/testing.md>), [unit-test](<https://devfeed.tech/tags/unit-test.md>)

## AI overview

This tutorial explains how to unit test private JavaScript functions while keeping them hidden from the public API. It discusses closures, shared browser scope, and a testing approach that preserves encapsulation without reducing test coverage.

## Source excerpt

JavaScript's closures provide an excellent way to make variables and functions private, keeping them out of the global scope. This is particularly important in the browser because all scripts share the same scope, and it's quite easy to inadvertently pick a variable or function name used by another library.