JavaScript Error - Undefined Object

Hi, I am learning JavaScript doing some simple tasks, but not quite sure why I keep getting the following error - bank-account-literal.html:44 Uncaught ReferenceError: Balance is not defined - yet I defined the balance in line 29?

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Bank Account</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <!-- linked styles -->
        <link rel="stylesheet" href="#">
        <!-- local styles -->
        <style>
            
        </style>
        <!-- linked scripts -->
        <script src="#">
        
        </script>
        <!-- local scripts -->
        <script>
            // js
        </script>
    </head>
    <body>
        <h1>Bank Account</h1>
        <!-- async scripts -->
        <script>
            var person1 = {
            name: 'Davidson',
            Number: 6657,
            Balance: 198.77
        };
            var account = function (name, Number, balance){
                this.name = name;
                this.Number = Number;
                this.balance = Balance;
            }
            account.prototype.deposit = function(amount){
                Balance + 100;
                console.info(`Deposit: ${name} new balance is ${Balance}`);
            }
            account.prototype.withdraw = function(amount){
                Balance - 50;
                console.info(`Withdrawl: ${name} new balance is ${Balance}`);
            }
            console.log('Hello ' + name, 'your account is '+ Number, 'with an outstanding balance of ' + Balance);
        </script>
    </body>
</html>

It isn’t defined anywhere. And capitalisation matters.

You aren’t creating the object anywhere: you define a function to create an object (account), which takes a name, a Number and balance.

And that person1 object you defined isn’t connected to anything, has nothing to do with the rest of the code

You create an account object by calling, for example,

var exampleAccount = new account("some name", 1, 100);

The object gets a property name set to “some name”, Number set to 1, and balance doesn’t work and errors because you’re trying to set it to Balance which isn’t a thing that exists (it’s supposed to be balance).

The console log can’t log anything because there are no variables called name or balance - these are things that only exist inside the object that you haven’t instantiated yet.

If you fix that you would access the properties like exampleAccount.name or exampleAccount.balance.

If it hadn’t already errored, it would log Number, because that’s a built-in JS object: don’t call your variables by the same name as built in objects, things will just break. If you really want a variable called “number” (would something like accountId not be better? Calling something number doesn’t explain what it is), then call it number

1 Like

Okay I understand thanks Dan. So would something like this be an example:

var account = {
  name: 'Davidson',
  number: 6657,
  balance: 198.77,
  deposit: function(amount) {
    this.balance += amount;
  },
}

Well, yes, but you’d already written the account function that does that more flexibly. With this you would have to write a new object with its own deposit method every time you wanted a new account