Using the console
suggest changeIn many environments, you have access to a global console object that contains some basic methods for communicating with standard output devices. Most commonly, this will be the browser’s JavaScript console (see Chrome, Firefox, Safari, and Edge for more information).
// At its simplest, you can 'log' a string
console.log("Hello, World!");
// You can also log any number of comma-separated values
console.log("Hello", "World!");
// You can also use string substitution
console.log("%s %s", "Hello", "World!");
// You can also log any variable that exists in the same scope.
// At the top level, this is the global object in a classic script and undefined in a module.
const arr = [1, 2, 3];
console.log(arr.length, this);
You can use different console methods to highlight your output in different ways. Other methods are also useful for more advanced debugging.
For more documentation, see the Console topic.
Current browsers provide console even when the developer tools are closed. In old Internet Explorer, console was missing until the tools were opened. Testing the bare name throws ReferenceError when it is missing. Check the type instead:
if (typeof console !== "undefined") {
console.log("test");
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents