Back to: JavaScript Tutorial For Beginners and Professionals
JavaScript Console Warn() Method with Examples
In this article, I am going to discuss JavaScript Console Warn() Method with Examples. Please read our previous article where we discussed JavaScript Console Trace() Method with Examples.
JavaScript console.warn() Method
The console.warn() method is used to print a warning message to the web console with a small triangle with an “i” icon displayed next to it. This method comes with a different color in the console tab. By default, the error message displays in yellow color in the console tab.
This is mostly used by NPM packages for angular/react framework to inform developers if the particular library or the API/version is deprecated or about any future changes to the library. Some web browsers add a small exclamation mark in the web console for warnings indication.
Syntax: console.warn(message);
Parameters:
- message – This parameter contains the warning message to write to the web console.
What we discussed above is given in the below example.
Example to Understand JavaScript Console Warn() Method
<html> <head> <title>JavaScript console warn() method example</title> </head> <body> <script> //sample code1 var student = { course: "JavaScript", price: "5000" }; console.warn(student); //sample code2 const myArr = ["apples", "oranges", "kiwi"]; console.warn(myArr); //sample code3 var year = 2022; console.warn('sample code3:', year); //sample code4 console.warn('This is sample warning message'); //sample code4 const printNum = number => { if (!number) { console.warn('number is not provided'); } }; printNum(); </script> </body> </html>
Output: Press F12 and go to the Console section
In the next article, I am going to discuss JavaScript Console Warn() Method with Examples. Here, in this article, I try to explain JavaScript Console Warn() Method with Examples. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.
About the Author:
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.