PUBLISHED ON: FEBRUARY 20, 2023
JavaScript Program to Write to the Console
JavaScript is a highly favored programming language that is extensively utilized for the creation of web-based applications. An important function in the arena of JavaScript development involves composing messages to the console. This feature offers developers the capacity to print messages, scrutinize and troubleshoot code, and engage in programmatic interaction. In the present article, we will see the process of composing messages to the console in JavaScript.
We also have an interactive JavaScript course where you can learn JavaScript from basics to advanced and get certified. Check out the course and learn more from here.
Using console.log()
The console.log()
method is used to write to the console. You can pass values directly into the method or pass a variable to write to a console.
- The
log()
method writes (logs) a message to the console.
- The
log()
method is useful for testing purposes.
// JavaScript Program to write to console
// passing number
console.log(8);
// passing string
console.log('JavaScript');
// passing variable
const x = 'JavaScript';
console.log(x);
// passing function
function sayName() {
return 'Hello JavaScript';
}
console.log(sayName());
// passing string and a variable
const name = 'JavaScript';
console.log('Hello ' + name);
// passing object
let obj = {
name: 'JavaScript',
age: 18
}
console.log(obj);
8
hello
hello
Hello JavaScript
Hello JavaScript
{
age: 28,
name: "JavaScript"
}
Conclusion
The JavaScript console program is a powerful application for developers, particularly when it comes to debugging. With the ability to log information, inspect objects, and test code snippets, the console can help you identify and fix issues in your code quickly. By using the simple program we provided, you can start writing to the console and exploring its many features today.