Intval
Intval will always return the sensible integer value of a given variable. For instance:
intval("1e10") === 10_000_000_000
but parseInt("1e10") === 1
intval("3.125e7") === 31_250_000
but parseInt("3.125e7") === 3
intval(true) === 1
and intval(false) === 0
whereas parseInt
would have returned NaN
in both cases but as you know:
parseInt(true) !== NaN !== parseInt(false)
though it isNaN()
;).
Unlike parseInt, intval
will never return NaN
. But you can provide a default value to be returned, in case the variable is undefined. Hence intval
eliminates the needs to check against NaN
and it helps you write even cleaner code.
Syntax:
intval(someValue)
intval(someValue, base)
intval(someValue, base, defaultValue)
This package is inspired by the PHP function intval, but this package is consistent in a javaScript way: for example in PHP
<?php intval("42", 8) === 34; ?>
but <?php intval(42, 8) === 42; ?>
whereas this package returns 34 in both cases.
So if it's supposed to be an integer, cast it with intval.
Installation
$ npm i intval
Usage
Require CommonJS (default)
const intval = require("intval");
Import ES-Module (default)
import intval from "intval";
Import ES-Module (named)
import { intval } from "intval";
more examples below)
Use in Code (let intValue = intval(someValue);
With base/radix
intval(someValue, base)
let intValue = intval(someValue, 2);
With default value for undefined variables
intval(someValue, base, defaulValue)
let intValue = intval(someValue, 10, 42);
is the same as
let intValue = typeof someValue != "undefined" ? intval(someValue) : 42;
Caution: The default value will not be type casted and the base/radix has no effect on it. So, the following will return a string value "42" in case 'someValue' is undefined
let myValue = intval(undefinedVariable, 16, "42");
// myValue === 66 --> false
// myValue === 42 --> false
// myValue === "42" --> true
Some examples
intval() === 0; // parseInt would have returned NaN
intval(null) === 0; // parseInt would have returned NaN
intval("") === 0; // parseInt would have returned NaN
intval("1e10") === 10_000_000_000; // parseInt would have returned 1
intval(1e10) === 10_000_000_000;
intval(true) === 1; // parseInt would have returned NaN
intval(false) === 0; // parseInt would have returned NaN
intval(42) === 42;
intval(4.2) === 4;
intval("42") === 42;
intval("+42") === 42;
intval("-42") === -42;
intval(042) === 34;
intval("042") === 42;
intval(0x1a) === 26;
intval("0x1A") === 26;
intval(42000000) === 42000000;
intval(420000000000000000000) === 420000000000000000000;
intval("420000000000000000000") === 420000000000000000000;
intval([]) === 0;
(intval(["22foo", "bar"]) === intval("22foo")) === 22; // same as parseInt, returns intval of the first array element. But in php intval(["22foo", "bar"]) === 1
intval(123_456) === 123456;
intval("123_456") === 123;
Pass in a base/radix as a second argument - just like with parseInt
intval(42, 8) === 34;
intval("42", 8) === 34;
intval(1011, 2) === 11;
intval("1g51", 16) === 1;
intval("1f51", 16) === 8017;
Pass in a default value in case the variable is undefined
As described above the base has no effect on the default value.
let someValue; // undefined
intval(someValue, 10, 42) === 42;
intval(someValue, 8, 42) === 42;
intval(someValue, 8, "42") === "42";
intval("1g51", 16, 42) === 1;
intval(someValue, 16, 42) === 42;
Testing (jest)
npm test
License
See LICENSE.
Copyright
Copyright © 2022. Kossi D. T. Saka.