JavaScript Console Assert() Method

JavaScript Console Assert() Method with Examples

In this article, I am going to discuss JavaScript Console Assert() Method with Examples. Please read our previous article where we discussed JavaScript Console Object.

JavaScripr console.assert() Method

The Console.Assert() method in JavaScript is used to write an error message to the console if an assertion fails. If an assertion is true, it will print nothing. The JavaScript console.assert() method is used to test if the passed argument is a truthy or falsy value. The values in JavaScript are categorized into truthy or falsy.

  1. All values are truthy unless they are defined as falsy
  2. Falsy value is a value that evaluates to FALSE

There are only 6 falsy values in JavaScript. They are as follows:

JavaScript Console Assert() Method with Examples

If the passed value is falsy in that case, the method logs/prints the extra arguments passed after the first argument, otherwise, the code execution continues without any log.

Syntax: console.assert(expression, message)

Parameters:
  1. expression – any expression. If an expression evaluates to false, an error message is written to the console. Else it will print nothing if the expression evaluates to true.
  2. message – The message to be displayed in the console.

What we discussed above is given in the below example.

Example to Understand JavaScript Console Assert() Method
<html>
<head>
    <title>JavaScript console assert() method example</title>
</head>
<body>
    <script>
        //sample code1
        function test() {
            a = -10;
            window.console.assert(a === -10, "a is not equal to -10"); //true
            console.assert(a >= 0, 'a is negative') //false
        }
        test();

        //sample code2
        let x = 1;
        let y = 2;
        console.assert(x + y == 11, "Expression returned 'false'"); //false

        //sample code3
        /*truthy value, nothing will be logged
         */
        console.assert(1, '1 is falsy value');
        console.assert(true, 'true is falsy value');
        console.assert('hello javascript', 'hello JavaScript is a falsy value');

        /*falsy value, assertion failed error logged
         */
        console.assert(0, '0 is falsy value');
        console.assert(false, 'false is falsy value');
        console.assert('', 'an empty string is a falsy value');
    </script>
</body>
</html>

Output: Run the above code, and then press the F12 key and go to the Console tab as shown in the below image.

Example to Understand JavaScript Console Assert() Method

In the next article, I am going to discuss JavaScript Console Clear() Method with Examples. Here, in this article, I try to explain JavaScript Console Assert() 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.

Leave a Reply

Your email address will not be published. Required fields are marked *