MOCKSTACKS
EN
Questions And Answers

More Tutorials









console.log()

All modern web browsers, Node.js as well as almost every other JavaScript environments support writing messages to a console using a suite of logging methods. The most common of these methods is console.log().
In a browser environment, the console.log() function is predominantly used for debugging purposes.


Open up the JavaScript Console in your browser, type the following, and press Enter :


console.log("Hello, World!");

Output

console.log() example

In the example above, the console.log() function prints Hello, World! to the console and returns undefined
(shown above in the console output window). This is because console.log() has no explicit return value.


Logging variables

console.log() can be used to log variables of any kind; not only strings. Just pass in the variable that you want to
be displayed in the console, for example:


var foo = "bar";
console.log(foo);

Output

Javsacript console.log() sample

If you want to log two or more values, simply separate them with commas. Spaces will be automatically added
between each argument during concatenation:


var thisVar = 'first value';
var thatVar = 'second value';
console.log("thisVar:", thisVar, "and thatVar:", thatVar);

Output

JS console.log() example and sample

Logging HTML elements


You have the ability to log any element which exists within the DOM. In this case we log the body element:


console.log(document.body);

Output

In output you can see all the HTML code that you have written for the particular page



Conclusion

In this page (written and validated by ) you learned about Javascript console.log() . What's Next? If you are interested in completing Javascript tutorial, your next topic will be learning about: Javascript console.time.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.