console.log()
18th January 2021
0 Comments
Programming Language
JavaScript
Syntax
Typical Usage
Outputs a message to the web console. The message may be a single string (with optional substitution values), or it may be any one or more JavaScript objects. Often used to verify that something is working and that the expected values are presented.
Accepted Parameters
item (mixed) (Required): The item you wish to log. You may have as many as you need, separating them by commas. If you need the variable names to be present, wrap them in curly braces.
Notes
When logging objects use console.log( JSON.parse( JSON.stringify(object) ) ) to get human readable output.
Examples of console.log()
Example 1
var data = {
'cars' : 2,
'bikes' : 15,
'boats' : 1
};
console.log(data);
The above will log a message in the console that resembles:
Object bikes: 15 boats: 1 cars: 2
Example 2: console.log Multiple Items
var firstname = 'John';
var lastname = 'Silver';
var assets = {
'cars' : 2,
'bikes' : 15,
'boats' : 1
};
console.log(firstname, lastname, assets );
The above will output:
John Silver {cars: 2, bikes: 15, boats: 1} bikes: 15 boats: 1 cars: 2
Example 3: console.log Multiple Items With Variable Names as Keys
var firstname = 'John';
var lastname = 'Silver';
var assets = {
'cars' : 2,
'bikes' : 15,
'boats' : 1
};
console.log( {firstname, lastname, assets} );
Placing curly braces around the variables like the above will output:
{firstname: "John", lastname: "Silver", assets: {…}} assets: bikes: 15 boats: 1 cars: 2 firstname: "John" lastname: "Silver"
Similar Functions in Other Languages
- PHP: print_r()
Was This Helpful?
If this post has helped you, please let me know. Your feedback will be highly appreciated and will help me build better content.