Learn some different ways to print to the console with JavaScript.
The standard way to get some output in your browser with JavaScript is to print to the console.
You can do this with the console.log function.
console.log('Logging to the console');
console.log(2 + 2);
const myVar = 'A message';
console.log(myVar);
console.log('Logging to the console', 2 + 2, myVar);
In the above examples you can see how the console.log function can be used to output any type of data in JavaScript.
All you need to do is call the function with a string, number or pretty much anything in JavaScript (like objects or arrays). You can pass in a variable also and you can log multiple things to the console simply by separating them with commas.
Other ways to print to the console with JavaScript
console.warn('This is a warning');
console.error('This is an error');
There are also a couple of other ways that you can print to the console with JavaScript. For example, using console.warn and console.error. They do the same thing as console.log but just add a bit of emphasis to the output (usually orange for warn and red for errors).
You can also log out objects and arrays with the console.table function:
console.table({ name: 'James', shoeSize: 10 });
Which provides some nicely formatted output if you’re trying to investigate all the properties of a particular object for example.
Watch the video to learn about how to use JavaScript to print to the console.