The _.truthy() method checks whether the given value is truthy or not, correspondingly returning a boolean value. A truthy value is one that coerces to true in a boolean context.
Syntax:
_.truthy( value );
Parameters:
- value: Given value to be checked for truthy value.
Return Value: This method returns a Boolean value (Returns true if the given value is truthy , else false).
Note: This will not work in normal JavaScript because it requires the underscore.js contrib library to be installed.
Underscore.js contrib library can be installed using npm install underscore-contrib –save.
Example 1: True boolean always returns true.
// Defining underscore contrib variable var _ = require( 'underscore-contrib' );
var bool = _.truthy( true );
console.log( "Given Value is Truthy : " ,bool);
|
Output:
Given Value is Truthy : true
Example 2: Existing values return a true boolean, hence this method returns true.
// Defining underscore contrib variable var _ = require( 'underscore-contrib' );
var bool = _.truthy(10);
console.log( "Given Value is Truthy : " ,bool);
|
Output:
Given Value is Truthy : true
Example 3:
// Defining underscore contrib variable var _ = require( 'underscore-contrib' );
var bool = _.truthy([ 1, 2, 3 ]);
console.log( "Given Value is Truthy : " ,bool);
|
Output:
Given Value is Truthy : true
Example 4: On passing false boolean, this method returns false.
// Defining underscore contrib variable var _ = require( 'underscore-contrib' );
var bool = _.truthy( false );
console.log( "Given Value is Truthy : " ,bool);
|
Output:
Given Value is Truthy : false