Thanks to visit codestin.com
Credit goes to coreui.io

How to log variables in JavaScript

Logging variables is fundamental for debugging JavaScript applications and understanding program flow during development. As the creator of CoreUI with over 25 years of JavaScript development experience, I’ve used console logging extensively for debugging complex UI interactions and data flows. From my expertise, the most effective approach is using console.log() with descriptive labels and leveraging advanced console methods for complex data structures. This practice provides immediate visibility into variable values and program execution.

Use console.log() with descriptive labels to output variable values for debugging and development.

const username = 'john_doe'
const userAge = 25
console.log('Username:', username, 'Age:', userAge)

Here console.log('Username:', username, 'Age:', userAge) outputs both variables with descriptive labels to the browser’s console. Multiple values can be passed as separate arguments, and the console automatically adds spaces between them. For objects and arrays, console.log() displays an interactive view that can be expanded. This method works in all browsers and provides immediate feedback during development and debugging sessions.

Best Practice Note:

This is the same approach we use in CoreUI development for debugging component state and prop values during development. Consider using console.table() for arrays and objects, or console.group() for organizing related log messages in complex debugging scenarios.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to check if a key exists in JavaScript object?
How to check if a key exists in JavaScript object?

What is CoreUI and Why Should You Use It for Your Next Admin Dashboard?
What is CoreUI and Why Should You Use It for Your Next Admin Dashboard?

Understanding the difference between `for...in` and `for...of` statements in JavaScript
Understanding the difference between `for...in` and `for...of` statements in JavaScript

What is the difference between typeof and instanceof in JavaScript
What is the difference between typeof and instanceof in JavaScript

Answers by CoreUI Core Team