JavaScript Print To Console
Last Updated : Mar 11, 2024
IN - JavaScript | Written & Updated By - Anjali
In this article we will show you the solution of JavaScript print to console, we will start by learning how to use it to display straightforward messages on the console. Then, we will look at how to use this approach to generate output variables and sophisticated data structures.
In addition, we'll show you how to arrange the output and use console.log() to do successful debugging. For providing data and debug for developers code, there is a handy tool in JavaScript called console.log() method.
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.
Using console.log()
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.
Printing Variables
Variable values can be printed by supplying them as parameters to the console.log() function.
String Formatting
JavaScript has a powerful feature that allows you to format strings with substitutes (%s for strings, %d as integers as well, %f for floating-point values, and so on). This is also possible with console.log().
Step By Step Guide On JavaScript Print To Console :-
// Simple messages Printing console.log("Hello, JavaScript World!"); // Printing variables let first_name = "Java"; let last_name = "Script"; console.log(first_name+last_name); // Printing complex data structures let person = { name: "XYZ", profession: "Engineer" }; console.log("Person : ", person); // Formatting the output let score = 90; console.log("Student : %s, Score : %d ", first_name, score);
- For printing the given string "Hello, JavaScript World!" on the console, we have used console.log() function. It is an easy example of printing a message.
- 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.
Conclusion :-
Fundamental tool, the console.log() method in JavaScript development. It is crucial for debugging and figuring out how code behaves because to its simplicity and adaptability.
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.
I hope this article on JavaScript print to console helps you and the steps and method mentioned above are easy to follow and implement.