Skip to content
the poor coder

console log object js

Poor Man
Published
Reading time
2 min read
On this page

Console Log Object in JavaScript#

When working with JavaScript, it is common to use objects to store and manipulate data. To debug and test your code, it is helpful to know how to log objects in the console. This can provide you with valuable information about the state of your program and help you identify any issues that may be present.

Using console.log()#

The simplest way to log an object to the console is by using the console.log() method. This method allows you to log any variable or expression to the console, including objects.

Text
const myObject = {
  name: "John",
  age: 30,
  city: "New York"
};

console.log(myObject);

The above code creates an object called myObject and logs it to the console using console.log(). When you run this code in your browser's console, you will see the object logged as a collapsible tree, which allows you to inspect the object and its properties.

Using JSON.stringify()#

If you need to log an object in a more readable format, you can use the JSON.stringify() method. This method converts a JavaScript object into a JSON-formatted string, which can be logged to the console.

Text
const myObject = {
  name: "John",
  age: 30,
  city: "New York"
};

console.log(JSON.stringify(myObject, null, 2));

The JSON.stringify() method takes three arguments: the object to convert, a replacer function (which we are not using in this example), and the number of spaces to use for indentation. In this example, we are using a value of 2 for indentation, which makes the output more readable.

Using console.dir()#

The console.dir() method is similar to console.log(), but it provides a more detailed output for objects. When you log an object using console.dir(), you will see a list of its properties and methods, along with their values and types.

Text
const myObject = {
  name: "John",
  age: 30,
  city: "New York"
};

console.dir(myObject);

The above code logs the myObject object to the console using console.dir(). When you run this code in your browser's console, you will see a detailed output for the object, including its properties and methods.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC