Declaring an Object in JS

I have the following code:

const todoList = {
    todos: []
};

I’m getting a reference error that todoList is not defined. I’ve looked up how an object is declared and I can’t find anything wrong here. I should be able to type todoList in console and see an object returned, correct?

Yes, but in what context are you doing this: are you literally trying that into the browser console (then on the next prompt typing todoList), or is it in a file, or in a function in a file etc.?

Using a script.js file in Visual Studio Code, and running an F5 test environment that launches a debug session in chrome and in VSC. Typing into the VSC console does not work, and opening Chrome dev tools on my localhost script.js page does not work either.

This appears to be an issue with running/testing the file locally, since I can work with the object using console.log and other methods with no problem. The object is defined correctly, I suppose I’m not sure why I can’t test this code in VSC or Chrome.

I want to post my solution here for future visitors. I was able to use Chrome console by adding an HTML file to my project, and linking my JS (script.js) file to the HTML file using the <script></script> tag. Once the HTML is open in Chrome (I use Preview on Web Server extension for Visual Studio Code) I can inspect the page, and use the console in Chrome to run commands and view console output.

Basically, I was trying to just open a .js file in Chrome, which shows you the code but does not run it as a script like the HTML page does with the <script> tag.

1 Like