Open In App

Underscore.js _.truthy() Method

Last Updated : 13 Aug, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Javascript




// 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.

Javascript




// 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: 

Javascript




// 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.

Javascript




// 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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads