JavaScript | console.log() with Examples

The console.log() is a function in JavaScript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user.

Syntax:

console.log(A);

Parameters: It accepts a parameter which can be an array, an object or any message.
Return value: It returns the value of the parameter given.
JavaScript codes to show the working of this function:

1) Passing a number as an argument: If the number is passed to the function console.log() then the function will display it.
Code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
var a = 2;
console.log(a);
</script>

chevron_right


Output:

2) Passing a string as an argument: If the string is passed to the function console.log(), then the function will display it.
Code #2:



filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
var str = "GeeksforGeeks";
console.log(str);
</script>

chevron_right


Output:

3) Passing a char as an argument: If the char is passed to the function console.log(), then the function will display it.
Code #3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
var ch = '2';
console.log(ch);
</script>

chevron_right


Output:

4) Passing a message as an argument: If the message is passed to the function console.log(), then the function will display the given message.
Code #4:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
console.log("GeeksforGeeks");
</script>

chevron_right


Output:

5) Passing a function as an argument: If the function is passed to the function console.log(), then the function will display the value of the passed function().
Code #5:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
function func() { return (5 * 19); }
console.log(func());
</script>

chevron_right


Output:

6) Passing a number with message as an argument: If the number is passed to the function console.log(), then the function will display it along with the given message.
Code #6:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
var a = 2;
console.log("The value of a is " + a);
</script>

chevron_right


Output:

7) Passing a string with message as an argument: If the string is passed to the function console.log(), then the function will display it along with the given message.
Code #7:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
var str = "GeeksforGeeks";
console.log("The value of str is " + str);
</script>

chevron_right


Output:

8) Passing a char with message as an argument: If the char is passed to the function console.log(), then the function will display it along with the given message.
Code #8:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
var ch = '2';
console.log("The value of ch is " + ch);
</script>

chevron_right


Output:



My Personal Notes arrow_drop_up

I like to do coding in C++C and java programming languages HTML and CSS always intersts me Sharing knowleged is the best way according to me to increase ones knwoledge

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.