當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


underscore.js _.isOdd()用法及代碼示例

這個_.isOdd()方法檢查給定值是否為奇數,相應地返回一個布爾值。

用法:

_.isOdd( value );

參數:

  • value:給定值,用於檢查奇數值。

返回值:此方法返回一個布爾值(如果給定值是奇數,則返回true,否則返回false)。

注意:由於它需要安裝underscore.js contrib庫,因此在正常的JavaScript中將無法使用。 Underscore.js contrib庫可以使用安裝npm安裝underscore-contrib



範例1:

Javascript

// Defining underscore contrib variable 
var _ = require('underscore-contrib');  
  
// Checking 
console.log("The Value is Odd:" + _.isOdd(41));

輸出:

The Value is Odd:true

範例2:

Javascript

// Defining underscore contrib variable 
var _ = require('underscore-contrib');  
  
// Checking 
console.log("The Value is Odd:" + _.isOdd(42));

輸出:

The Value is Odd:false

範例3:對於值0,此方法返回false。

Javascript

// Defining underscore contrib variable 
var _ = require('underscore-contrib');  
  
// Checking 
console.log("The Value is Odd:" + _.isOdd(0));

輸出:

The Value is Odd:false

範例4:對於非整數值,此方法返回false。

Javascript

// Defining underscore contrib variable 
var _ = require('underscore-contrib');  
  
// Checking 
console.log("The Value is Odd:" + _.isOdd("GFG"));

輸出:

The Value is Odd:false

相關用法


注:本文由純淨天空篩選整理自taran910大神的英文原創作品 Underscore.js _.isOdd() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。