JavaScript console.assert() Method
You are Here:
JavaScript console.assert() Method
The console.assert()
method writes an error message to the console if the assertion is false. If the assertion is true, nothing happens.
Note: This feature is available in Web Workers.
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
console.assert(true, "I will not display");
console.assert(false, "I will display");
</script>
</body>
</html>
Syntax
console.assert(expression, msg)
Parameter Values
Value | Type | Explanation |
---|---|---|
expression | Required | Any boolean expression. If the assertion is false, the message is written to the console. |
msg | Required | A JavaScript string containing zero or more substitution strings. |
Real Time Example
In the following example, we will try to get an id of an element which is not available.
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
var x = document.getElementById("point");
console.assert(x, "id = 'point' not found");
</script>
</body>
</html>
Reminder
Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in JavaScript.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.