When I'm writing code and haven't quite finished it yet, I like to test out the logic to make sure everything's working as it should. That's where the console.log()
method comes in handy.
Here's a simple guide to using the JavaScript console.log() method:
Syntax
console.log()
Examples
It writes a message to the browser console, which can help you debug and test your JavaScript code.
Basic usage:
console.log("Hello"); // Console --> Hello
console.log(1 + 2); // Console --> 3
You can log multiple arguments:
console.log("Hello", 1 + 2);
// Console --> Hello 3
You can log objects:
const user = { name: "John", age: 30 };
console.log(user);
// Console --> { name: "John", age: 30 }
You can format your logs with template literals:
const name = "John";
console.log(`Hello, my name is ${name}!`);
// Console --> Hello, my name is John!
You can log your feature to debug:
// Create a function test() that logs out the content of an array
// Add console log to verify that the loop is working
// Call/invoke the function
const array = [1, 2, 3]
function test() {
for (let = i; i < array.length; i++){
console.log(array[i])
}
test()
// Console --> display the content of the array. Correct.
// So the function is working.
In conclusion, the console.log()
method is a versatile tool that can help you debug and test your JavaScript code. Whether you're logging a simple message or a complex object, console.log()
is a great way to quickly check that your code is working as expected. Thanks for reading, and happy coding! 🥳