JavaScript console.log() Method
The console.log() method in JavaScript logs messages or data to the console, facilitating debugging by displaying variable values, object properties, or program flow information.
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.
These are the following ways to use the console.log() method:
Table of Content
- Passing a number as an argument
- Passing a string as an argument
- Passing a char as an argument
- Passing a message as an argument
- Passing a function as an argument
- Passing a number with the message as an argument
- Passing a string with the message as an argument
- Passing a char with the message as an argument
1. Passing a number as an argument
If the number is passed to the function console.log() then the function will display it.
Example:
let a = 2;
console.log(a);
Output:
2. Passing a string as an argument
If the string is passed to the function console.log(), then the function will display it.
Example:
let str = "w3wiki";
console.log(str);
Output:
3. Passing a char as an argument
If the char is passed to the function console.log(), then the function will display it.
Example:
let ch = '2';
console.log(ch);
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.
Example:
console.log("w3wiki");
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.
Example:
function func() { return (5 * 19); }
console.log(func());
Output:
6. 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:
let a = 2;
console.log("The value of a is " + a);
Output:
7. 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:
let str = "w3wiki";
console.log("The value of str is " + str);
Output:
8. 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:
let ch = '2';
console.log("The value of ch is " + ch);
Output: