What is “assert” in JavaScript?

What is “assert” in JavaScript?

What does assert mean in JavaScript?
I’ve seen something like:
assert(function1() && function2() && function3(), “some text”);

And would like to know what the method assert() does.

Solutions/Answers:

Solution 1:

There is no assert in JavaScript (yet; there’s talk of adding one, but it’s at an early stage). Perhaps you’re using some library that provides one. The usual meaning is to throw an error if the expression passed into the function is false; this is part of the general concept of assertion checking. Usually assertions (as they’re called) are used only in “testing” or “debug” builds and stripped out of production code.

Suppose you had a function that was supposed to always accept a string. You’d want to know if someone called that function with something that wasn’t a string. So you might do:

assert(typeof argumentName === "string");

…where assert would throw an error if the condition were false.

Related:  How do I get the number of days between two dates in JavaScript?

A very simple version would look like this:

function assert(condition, message) {
    if (!condition) {
        throw message || "Assertion failed";
    }
}

Better yet, make use of the Error object if the JavaScript engine supports it (really old ones might not), which has the advantage of collecting a stack trace and such:

function assert(condition, message) {
    if (!condition) {
        message = message || "Assertion failed";
        if (typeof Error !== "undefined") {
            throw new Error(message);
        }
        throw message; // Fallback
    }
}

Even IE8 has Error (although it doesn’t have the stack property, but modern engines [including modern IE] do).

Solution 2:

If using a modern browser or nodejs, you can use console.assert(expression, object).

For more information:

Solution 3:

The other answers are good: there isn’t an assert function built into ECMAScript5 (e.g. JavaScript that works basically everywhere) but some browsers give it to you or have add-ons that provide that functionality. While it’s probably best to use a well-established / popular / maintained library for this, for academic purposes a “poor man’s assert” function might look something like this:

const assert = function(condition, message) {
    if (!condition)
        throw Error('Assert failed: ' + (message || ''));
};

assert(1 === 1); // Executes without problem
assert(false, 'Expected true');
// Yields 'Error: Assert failed: Expected true' in console

Solution 4:

assert() is not a native javascript function. It is a custom function someone made. You will have to look for it on your page or in your files and post it for anybody to help determine what it’s doing.

Related:  Correct way to define an empty dom element in React

Solution 5:

check this:http://net.tutsplus.com/tutorials/javascript-ajax/quick-tip-quick-and-easy-javascript-testing-with-assert/

it is for testing JavaScript. Amazingly, at barely five or six lines, this code provides a great level of power and control over your code, when testing.

The assert function accepts two parameters:

outcome: A boolean, which references whether your test passed or failed

description: A short description of your test.

The assert function then simply creates a list item, applies a class of either “pass” or “fail,” dependent upon whether your test returned true or false, and then appends the description to the list item. Finally, that block of coded is added to the page. It’s crazy simple, but works perfectly.

Solution 6:

Here is a really simple implementation of an assert function. It takes a value and a description of what you are testing.

 function assert(value, description) {
        var result = value ? "pass" : "fail";
        console.log(result + ' - ' +  description); 
    };

If the value evaluates to true it passes.

assert (1===1, 'testing if 1=1');  

If it returns false it fails.

assert (1===2, 'testing if 1=1');