---
title: "Discussion on \"Understanding Private Variables in JavaScript Class Constructors\" | Hashnode"
description: "Discussion on \"Understanding Private Variables in JavaScript Class Constructors\". This week, my class has been looking into factory functions and classes. Naturally, the topic of 'private variables' came up, and has been a source of much confusion. To me, this confusion is similar to the confusion over Closure, which I addressed i..."
url: "https://hashnode.com/posts/understanding-private-variables-in-javascript-class-constructors/5fa1479124a897066ab25a37"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Understanding Private Variables in JavaScript Class Constructors](https://blog.annamcdougall.com/understanding-private-variables-in-javascript-class-constructors?utm_source=hashnode&utm_medium=feed)

This week, my class has been looking into factory functions and classes. Naturally, the topic of 'private variables' came up, and has been a source of much confusion. To me, this confusion is similar to the confusion over Closure, which I addressed i...

Nov 3, 2020 · 4 min read · blog.annamcdougall.com  
Read the article

-   [#javascript](https://hashnode.com/tag/javascript)
-   [#classes](https://hashnode.com/tag/classes)
-   [#variables](https://hashnode.com/tag/variables)

## Written by

[Anna J McDougall](https://hashnode.com/@AnnaJMcDougall)

@AnnaJMcDougall · Leipzig, Germany

  
[View profile](https://hashnode.com/@AnnaJMcDougall)

Full-stack Developer

## Responses 1

Markdown supported · ⌘↵ to post

[CJ](https://hashnode.com/@cjreads665)

[Jul 13, 2021](https://hashnode.com/posts/understanding-private-variables-in-javascript-class-constructors/5fa1479124a897066ab25a37/comment/60ed726cfc37a15ec1567c8e)

This really helped me understand what this "private variable" was really about. Thank you, Anna, for this wonderful explanation. just one thing:

```
class User {
  constructor(name, age, favNumber) {
    let superSecretNumber = Math.round((age * favNumber) / name.length);
    function logOutcome() {
      console.log(`Wow! Your secret number is ${superSecretNumber}`);
    }
    return { name, age, favNumber, logOutcome };
  }
}

let firstUser = new User("Anna", 33, 7);
console.log(firstUser.name); // logs "Anna"
console.log(firstUser.superSecretNumber); // WHOMP WHOMP: 'undefined'
firstUser.logOutcome();  // logs "Wow! Your secret number is 58"
```

Isn't the constructor function ONLY for assigning arguments to variables we use inside the class? shouldn't we declare the supersecretnumber outside the constructor? and why are assigning the values _outside_ the constructor?
