Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript | console.log() with Examples

  • Difficulty Level : Easy
  • Last Updated : 22 Nov, 2021

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:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

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:






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

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:




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

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:




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

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:




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

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:




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

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:




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

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:




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

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:




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

Output:

Supported Browser:

  • Chrome 1 and above
  • Edge 12 and above
  • Firefox 4 and above
  • Internet Explorer 8 and above
  • Opera 10.5 and above
  • Safari 3 and above

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!