Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

js 正整数

作者:硝烟   发布日期:2025-07-13   浏览:21

// 判断一个数是否为正整数的示例代码

function isPositiveInteger(num) {
    // 检查 num 是否为数字类型,并且是大于 0 的整数
    return typeof num === 'number' && Number.isInteger(num) && num > 0;
}

// 示例用法
console.log(isPositiveInteger(5));    // 输出: true
console.log(isPositiveInteger(-3));   // 输出: false
console.log(isPositiveInteger(0));    // 输出: false
console.log(isPositiveInteger(3.14)); // 输出: false
console.log(isPositiveInteger('5'));  // 输出: false

解释说明:

  • typeof num === 'number':确保传入的参数是数字类型。
  • Number.isInteger(num):检查该数字是否为整数。
  • num > 0:确保该整数是正数。

这个函数可以用来判断一个数是否为正整数。

上一篇:js 判断正整数

下一篇:js 整数正则

大家都在看

js 数组对象排序

js 数组删掉第一个值

js fill

js json数组

js 数组复制

js 复制数组

js 数组拷贝

js 对象转数组

js 深拷贝数组

js 点击空白区域触发事件

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站