Функция Underscore.js _.isBoolean()
Underscore.js — это библиотека JavaScript, которая предоставляет множество полезных функций, таких как отображение, фильтрация, вызовы и т. д., даже без использования каких-либо встроенных объектов.
Функция _isBoolean используется для определения того, является ли переданный элемент истинным / ложным или чем-то еще . Логические значения — это подмножество алгебры, которое используется для создания истинных/ложных утверждений.
Если элемент имеет значение true или false, то вывод будет истинным, в противном случае вывод будет ложным.
Он используется, когда нам нужно различать элементы, которые имеют значения true или false, и другие элементы, которые не имеют значений true/false.
Синтаксис:
_.isBoolean(object)
Параметры:
Он принимает только один аргумент, который является объектом, значение которого необходимо проверить.
Возвращаемое значение: возвращает true, если значение объекта равно true, или false, в противном случае возвращает false.
- Passing a variable having number value to the _.isBoolean() function:
The _.isBoolean() function takes the argument passed and then checks it’s 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 output is true otherwise the output is false.
Example:<
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
>
<
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 _.Boolean() 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
>
<
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
>
<
head
>
<
script
src
=
</
script
>
</
head
>
<
body
>
<
script
type
=
"text/javascript"
>
console.log(_.isBoolean(null));
</
script
>
</
body
>
</
html
>
Output:
ПРИМЕЧАНИЕ:
Эти команды не будут работать в консоли Google или в Firefox, так как необходимо добавить эти дополнительные файлы, которых они не добавили.
Итак, добавьте указанные ссылки в свой HTML-файл, а затем запустите их.
Ссылки следующие:
< script type = "text/javascript" src = </ script > |