Table of contents
1.
Introduction
2.
What is JavaScript console.assert() Method
3.
Syntax of JavaScript console.assert() Method
4.
Parameters of JavaScript console.assert() Method
5.
Some Important Points of JavaScript console.assert() Method
6.
Example 1
6.1.
HTML
7.
Example 2
7.1.
HTML
8.
Frequently Asked Questions
8.1.
What is a prompt box in JavaScript?
8.2.
What are callbacks?
8.3.
What is Hoisting in Javascript?
8.4.
What is implicit type coercion in JavaScript?
9.
Conclusion
Last Updated: Mar 27, 2024
Medium

JavaScript console.assert() Method

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The console function is a JavaScript object that allows developers to interact with the browser's debugging console. The console function is used to display messages, warnings, errors and other information to assist developers in debugging their code.

JavaScript console assert() Method

In this tutorial, you will get to about JavaScript console.assert function, its syntax, parameter description and some working code examples.

What is JavaScript console.assert() Method

The javascript console. assert() is used to display an error message or a message when a condition in the console beneath the inspect element does not match or the expression returns a False boolean value. The webpage UI has an alert mechanism for displaying issues.

This javascript function takes two parameters. The expression is the first parameter, while the message is the second. Both of these options are required for the console to function properly. In Javascript the assert() method is used.

This method just determines whether or not the supplied condition in the javascript assert method is true. The console. assert() method does not stop the application from running, it does not correct the error and it is not used in error management. It only appears if the message is specified in the method's message parameter on the console. This is not a technique for program correction.

Syntax of JavaScript console.assert() Method

The javascript console.assert() method has the following syntax:

console.assert(expression, message)


There are two parameters of the javascript console.assert() method.The expression is the first parameter of the javascript assert() method, and the message is the second. Both of these parameters are required for the console.assert() method in javascript to function properly.

Parameters of JavaScript console.assert() Method

The parameter of the javascript console. assert() method is as follows:

  • Expression: This method requires the expression as an argument. It represents any evaluateable expression. When this method is called in a program, as soon as the execution reaches the expression parameter, the expression is evaluated, and the next message is displayed on the user terminal based on the result. This expression returns simply a boolean value. If the expression's result is True, the message is not printed; if the expression's result is False, the message is printed on the console.
     
  • Message: The message is also required by the JavaScript console.assert() method. It comprises the information that must be printed on the console. When the execution reaches the message parameter, it operates in accordance with the result of the expression parameter. If the expression's result is True, the message is not printed; if the expression's result is False, the message is printed on the console.

Some Important Points of JavaScript console.assert() Method

  • The return value of the javascript console.assert() method is none or an undefined value.
     
  • The message parameter is a string or it may be an object that is written on the console.
     
  • If the condition is false, the javascript console.assert() method throws an error message and shows it on the console. If the condition is true, the javascript assert method has no effect.
     
  • Almost all modern browsers implement the javascript console. assert() method. The names of some major browsers that support the console. assert() method are listed below.

Example 1

In this example, we will write a message to the console using a console.assert method.

Code:

  • HTML

HTML

<!DOCTYPE html>
<html>
<style>
body {
background-color: #e4e4e4;
}
h1,
h2 {
color: red;
display: inline-block;
width: 50%;
}
p {
font-size: 18px;
}
</style>
<body>
<h1>Example 1</h1>
<h2>Working of JavaScript console.assert() Method</h2>
<p>
Make sure to open the console, either manually or by Pressing F12
before you click "Run".
</p>

<script>
let x = 10;
let y = 20;
console.assert(x + y == 30, "Expression returned true");
</script>
</body>
</html>

 

Output:

console.assert() example 1

In the script portion of the above code, two variables, x, and y, are assigned values of 10 and 20. The console.assert() method determines whether their sum equals 30. If true, no console output is produced; if false, an error is recorded. Shows how to use console.assert() to debug and validate JavaScript expressions.

Example 2

In this example, we will display an object by the use of the console.assert() method.

Code:

  • HTML

HTML

<!DOCTYPE html>
<html>
<head>
<title>DOM console.assert() Method</title>
<style>
body {
background-color: #e4e4e4;
}
h1,
h2 {
color: red;
display: inline-block;
width: 50%;
}
p {
font-size: 18px;
}
</style>
</head>
<body>
<h1>Example 2</h1>
<h2>DOM console.assert( ) Method to display object</h2>
<p>
To view the message in the console press the F12 key on your
keyboard or open manually
</p>
<script>
var MyElement = { Product: "Burger", Type: "Junk Food" };
console.assert(document.getElementById("MyDemo"), MyElement);
</script>
</body>
</html>

 

Output:

console.assert() example 2

The script in the above code generates an object called MyElement with the values Product and Type to represent a type of food. The console.assert() method is then used to determine whether an element with the id MyDemo exists in the document's DOM. If the element is not found, the browser console displays an assertion error along with the MyElement object which contains information about the food product.

Frequently Asked Questions

What is a prompt box in JavaScript?

It shows a dialog box with a message that the user may choose to enter text into. When a user wishes to enter a value before moving on to the next page, they typically utilize this method. Either a string containing the user's content or null is returned.

What are callbacks?

A callback is a function that will run after another function has finished running. Functions are first-class citizens in javascript; they can be used as an object's property or as an argument to another function. They can also be returned by another function.

What is Hoisting in Javascript?

Hoisting is the default behavior of javascript, which moves all variable and function declarations to the top. This indicates that the variables and functions are relocated to the top of the scope regardless of where they are declared. The scope of the project might be both local and worldwide.

What is implicit type coercion in JavaScript?

Implicit type coercion in javascript is the automatic conversion of value from one data type to another. It takes place when the operands of an expression are of different data types. When JavaScript notices that the operands of the expression x + y are of different kinds, it converts the number type to the string type.

Conclusion

JavaScript console.assert() method helps with debugging by verifying expressions. It checks the conditions and logs errors if they are false. This improves development efficiency and ensures dependable operation while also assisting in code correctness and troubleshooting. In this article, we will discuss what is the JavaScript console.assert() method, some important points, and its working examples.

To better understand the topic, you can refer to JavaScript Console LogUsing Functions in JavaScript, and Operators in Javascript.

For more information, refer to our Guided Path on Coding Ninjas Studio to upskill yourself in PythonData Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more! 

Head over to our practice platform, CodeStudio, to practice top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!

Live masterclass