The Underscore.js is a JavaScript library that provides a lot of useful functions like the map, filter, invoke, etc even without using any built-in objects. The _isBoolean function is used to find whether the element passed is true/ false or something else. Boolean is a subset of algebra that is used to create true/false statements. If the element has a value of either true or false then the output will be true otherwise its output will be false. It is used when we have to distinguish between the elements which have true or false values and the other elements which do not have true/false as their values.
Syntax:
_.isBoolean(object)
Parameters: It takes only one argument which is the object whose value needs to be checked.
Return value: It returns true when the object’s value is true or false otherwise it returns false.
Passing a variable having number value to the _.isBoolean() function: The _.isBoolean() function takes the argument passed and then checks its value. It checks the argument’s value by comparing the value with both ‘true’ and ‘false’. If it is matched by any one of these, then the output is true otherwise the output is false.
Example:
html
< html > < head > < script src= </ script > </ head > < body > < script type="text/javascript"> var a = 10; console.log(_.isBoolean(a)); </ script > </ body > </ html > |
Output:
Passing a variable having ‘false’ as it’s value to the _.isBoolean() function: If we pass an element that has ‘false’ assigned to it then also the same procedure as the previous will be followed. The argument’s value will be compared to both ‘true’ and ‘false’. Since it’s value is false, so it will be matched and hence the output will be true.
Example:
HTML
< html > < head > < script src= </ script > </ head > < body > < script type="text/javascript"> var a = false; console.log(_.isBoolean(a)); </ script > </ body > </ html > |
Output:
Passing ‘true’ to _.isBoolean() function: The _.isBoolean() function, in this case, does not need to check the variable’s value as no variable is passed as an argument rather the value itself is passed. The value will be matched directly to ‘true’ and ‘false’. Since the passed argument is ‘true’ so it will be matched and hence the output will be true.
Example:
html
< html > < head > < script src= </ script > </ head > < body > < script type="text/javascript"> console.log(_.isBoolean(true)); </ script > </ body > </ html > |
Output:
Passing ‘null’ to the _.isBoolean() function: When we pass null value to the _.isBoolean() function then no error is generated rather the same checking procedure will be followed. Since after matching the null value to both the true and false, it will not be matched, so, the output will be false.
Example:
HTML
< html > < head > < script src= </ script > </ head > < body > < script type="text/javascript"> console.log(_.isBoolean(null)); </ script > </ body > </ html > |
Output:
NOTE:
These commands will not work in Google Console or in Firefox as these additional files need to be added which they didn’t have added.
So, add the given links to your HTML file and then run them.
The links are as follows:
html
< script type= "text/javascript" src = </ script > |