JavaScript console.log() Method

JavaScript's console.log() function is used to print any variables that have been defined in the program or to display any other message to the user.

Syntax:

console.log(" ");

Parameters: It accepts a parameter, which may be an object, an array, or any message.

Return value: This function returns the value of the given parameter.

Here are some examples of JavaScript code that show how this function works:

A number as an argument: The console.log() function will display the number if a number is given as an argument.

Example 1:

var a = 10;

console.log(a);


Output:

String as an argument: If a string is supplied as an argument to the console.log() function, the function will show the string.

Example 2:

var str = "Hello World";

console.log(str);

Output:

A char as an argument: If a char is given as an argument to the console.log(), then it will be displayed by the function.

Example 3:

  var ch = '15';

  console.log(ch);

Output:

A message as an argument: The function will display the message if the console.log() function is passed with the message.

Example 4:

  console.log("Hello World");

Output:

A function as an argument: If a function is provided as an argument to the function console.log(), it will display the value of the given function.

Example 5:

  function func() { return (21*33); }

  console.log(func());

Output:

A number with a message as an argument: When a number is supplied to the console.log() function, then it will display the number along with the given message.

Example 6:

  var a = 2;

  console.log("The value of a is " + a);

Output:

A message with string as an argument: If the string is submitted to the console.log() function, it will be displayed alongside the given message.

Example 7:

  var str = "Hello World";

  console.log("The value of str is " + str);

Output:

A message with char as an argument: If the char is submitted to the console.log() function, it will be displayed alongside the given message.

Example 8:

  var ch = '2';

  console.log("The value of ch is " + ch);

Output:

The console.log() function is supported by the below browsers:

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

Javascript is mostly used in creating web pages, but it is a versatile language and can be used for a wide range of applications, such as developing web apps and mobile apps for Android and iOS.