The Wayback Machine - https://web.archive.org/web/20230503204849/https://www.geeksforgeeks.org/javascript-console-log-method/
Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

JavaScript console.log() Method

Improve Article
Save Article
Like Article
author
Sakshi98
scholar
82 published articles
Improve Article
Save Article
Like Article

The console.log() is a function in JavaScript that 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("");

Parameters: It accepts a parameter that can be an array, an object, or any message. 

Return value: It returns the value of the parameter given. 

Below examples illustrate the JavaScript console.log() Method:

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

Example 1: 

javascript




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

Output:

  

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

 Example 2: 

javascript




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

Output:

  

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

Example 3: 

javascript




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

Output: 

 

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

 Example4: 

javascript




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

Output:

  

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.

 Example 5: 

javascript




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

Output:

  

Passing a number with the 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. 

Example 6: 

javascript




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

Output:

 

Passing a string with the 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. Example 7: 

javascript




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

Output:

  

Passing a char with the 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. Example 8: 

javascript




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

Output:

  

Supported Browser: It is supported by all browsers

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
Last Updated : 09 Jan, 2023
Like Article
Save Article
Similar Reads
Related Tutorials