Loading...

Console.log in JavaScript – Complete Guide

Console.log in JavaScript – Complete Guide

If you are aspiring to become a pro web developer, you need to have a strong understanding of the basics of HTML, CSS, and JavaScript. JavaScript is widely used for the front-end and back-end development of web pages and mobile applications. Today’s article is going to be a tutorial on one of the most basic and most used methods that are console.log in JavaScript. 

What is JavaScript?

Before we jump into console.log in JavaScript, let us understand JavaScript and why it is so important in the tech industry. JavaScript is used by developers all around the world to build beautiful interactive user components for websites. Around 97% of the website currently make use of JavaScript. Many technologies like React JS, Next.js, Node.js, and so on are developed in JavaScript. Furthermore, all these libraries and frameworks are in demand right now hence learning JavaScript is a prerequisite to knowing these technologies.

If you have no clue where you should start your JavaScript journey, then make sure to check out Codedamn! The courses here will have you develop a strong basic understanding of many technologies from React JS to Solidity to even git and chrome dev tools. Additionally, the blogs posted here will keep you contemporized with the latest tech sensations. 

Where is console.log in JavaScript?

Now that we have an idea of how important JavaScript is, let us jump into console.log in JavaScript. In JavaScript, we have the window object which represents the open window of a browser. This object has many properties which help the developer control different functions of the browser. For instance, the closed property returns a Boolean true if the window is closed.

One of the properties of the window object is the console object. It provides the developer access to the debugging console of the browser. One can make use of it either by the window.console or simply by console. Here are some examples.

window.console.error(“Oops! You made a mistake”);
console.error(“Oops! You made a mistake”);

The console object has many methods such as the clear() method which helps you clear the console. Then there is the error() method which we implemented in the previous example. This method helps to display the error message on to the console. Then, there are other methods such as time(), trace(), table(), and so on. But today we will discuss console.log in JavaScript. 

What is console.log in JavaScript?

Now that you know from where console.log in JavaScript, let us understand what this method does. Console.log in JavaScript is used to print on the console. Here is the syntax for the same.

console.log(“Hello World”);
console.log in JavaScript
console.log in JavaScript

The main two functions of console.log in JavaScript are to display a message on the console and for testing the code. The parameters that this function can accept are an array, object, or any sort of message. It returns the value of the given parameter to the console.

Examples of console.log in JavaScript

Here are some code examples to show the functioning of console.log in JavaScript.

1) Passing an integer as a parameter:

If a number or an integer is given as an argument to the console.log() method, then it will display the value of that number.

<script>
var n = 10;
console.log(n); // expected output: 10
</script>

2) Passing a string or a character as a parameter:

If a string or a char is given as input then the console.log() will display that string or that char.

<script>
var string1 = “Codedamn”;
console.log(string1); // expected output: Codedamn
</script>
<script>
var ch = ‘6’;
console.log(ch); // expected output: 6
</script>

3) Passing a message as a parameter:

One can directly pass a message through the console.log in JavaScript.

<script>
console.log(“Codedamn”); // expected output: Codedamn
</script>

4) Passing a function as a parameter:

If a function is passed through console.log in JavaScript. Then it will run that function and display the value that is returned. Note that the function should be defined before calling it otherwise you will get an error.

<script>
function func() { return (10 + 10); }
console.log(func()); // expected output: 20
</script>

5) Passing a variable with a message:

One can pass a variable with a message as well using console.log in JavaScript. But note that the variable show is declared and given some value before it is been used. Here are some examples.

<script>
var n = 10;
console.log(“Example of number and message ” + n);
// expected output: Example of number and message 10
</script>
<script>
var string1 = “Codedamn”;
console.log(“Example of string and message ” + string1);
// expected output: Example of string and message Codedamn
</script>
<script>
var ch = “8”;
console.log(“Example of char and message ” + ch);
// expected output: Example of char and message 8
</script>

The console.log() method is supported by Chrome, Edge (12 and above), Firefox (4 and above), Safari (3 and above), Opera(10.5 and above), and Internet Explorer (8 and above).

Conclusion

If you liked this tutorial on console.log in JavaScript, please check out Codedamn for the javascript course and their courses and their articles. Also please do not forget to subscribe to our YouTube channel and join our amazing community!

Sharing is caring

Did you like what Agam singh, Aman Ahmed Siddiqui, Aman Chopra, Aman, Amol Shelke, Anas Khan, Anirudh Panda, Ankur Balwada, Anshul Soni, Arif Shaikh, wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far