Last Updated : Sep 26, 2023
Whether you are a novice or a seasoned programmer, mastering the use of console.log() can boost your productivity and comprehension of the code's behaviour during runtime.
This article intends to provide a full lesson on utilizing console.log() for writing to the console, as well as to explore its features and demonstrate how it simplifies debugging.
The console.log() method is a very often used way for printing to the console. It enables us to send messages, variables, or other data via the console.
Variable values can be printed by supplying them as parameters to the console.log() function.
- With the let keyword, the variable first_name is declared, and the value "Java" is given to it.
- Another variable entitled last_name is declared and given the value "Script" in this statement.
- console.log(first_name+last_name); - The first_name and last_name variables' values are concatenated and printed using the console.log() function in this line. String concatenated is accomplished with the + operator.
- Name and occupation are the two characteristics of the object "person," which is defined in this way.
- The string "Person:" is printed in this line, next to the person object, using the console.log() function. The console.log() function's, provides for the printing of numerous arguments.
- With this, a variable called score is declared and given the value 90.
- To format the output, use placeholders on this line. A text placeholder is represented by %s, and a numeric placeholder is shown by %d. The values for first_name and score are used in place of the placeholders.
In this article, we looked at the fundamentals of utilizing console.log() to output variables, print straightforward messages, and display intricate structures for data like objects.
Furthermore, we discovered how to arrange the output with placeholders, improving the legibility of the data sent to the console.