--- warning: 'This file is auto generated by `npm run web:inject`, do not edit by hand' examples: - intval('Kevin van Zonneveld') - intval(4.2) - 'intval(42, 8)' - intval('09') - 'intval(''1e'', 16)' - intval(0x200000001) - 'intval(''0xff'', 0)' - 'intval(''010'', 0)' estarget: es5 returns: - '0' - '4' - '42' - '9' - '30' - '8589934593' - '255' - '8' dependencies: [] authors: original by: - 'Kevin van Zonneveld (http://kvz.io)' improved by: - stensi bugfixed by: - 'Kevin van Zonneveld (http://kvz.io)' - 'Brett Zamir (http://brett-zamir.me)' - 'Rafał Kukawski (http://blog.kukawski.pl)' input by: - Matteo notes: [] type: function layout: function title: PHP's intval in JavaScript description: >- Here’s what our current JavaScript equivalent to PHP's intval looks like. function: intval category: var language: php permalink: php/var/intval/ alias: - /functions/php/intval/ - /functions/var/intval/ - /php/intval/ - /functions/intval/ --- {% codeblock lang:javascript %}module.exports = function intval (mixedVar, base) { // discuss at: http://locutus.io/php/intval/ // original by: Kevin van Zonneveld (http://kvz.io) // improved by: stensi // bugfixed by: Kevin van Zonneveld (http://kvz.io) // bugfixed by: Brett Zamir (http://brett-zamir.me) // bugfixed by: Rafał Kukawski (http://blog.kukawski.pl) // input by: Matteo // example 1: intval('Kevin van Zonneveld') // returns 1: 0 // example 2: intval(4.2) // returns 2: 4 // example 3: intval(42, 8) // returns 3: 42 // example 4: intval('09') // returns 4: 9 // example 5: intval('1e', 16) // returns 5: 30 // example 6: intval(0x200000001) // returns 6: 8589934593 // example 7: intval('0xff', 0) // returns 7: 255 // example 8: intval('010', 0) // returns 8: 8 var tmp, match var type = typeof mixedVar if (type === 'boolean') { return +mixedVar } else if (type === 'string') { if (base === 0) { match = mixedVar.match(/^\s*0(x?)/i) base = match ? (match[1] ? 16 : 8) : 10 } tmp = parseInt(mixedVar, base || 10) return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp } else if (type === 'number' && isFinite(mixedVar)) { return mixedVar < 0 ? Math.ceil(mixedVar) : Math.floor(mixedVar) } else { return 0 } } {% endcodeblock %}