In JavaScript, numbers are one of the most commonly used data types. Whether you’re calculating totals, handling user input, or manipulating data — you’ll use numbers constantly.
What makes JavaScript unique is that it doesn’t differentiate between integers and floating-point numbers at the type level.
All numeric values in JavaScript are of the type number.
Example:
let x = 42;       // integer
let y = 3.14;     // float
console.log(typeof x); // "number"
console.log(typeof y); // "number"
Both x and y are numbers — JavaScript treats them the same internally.
JavaScript Integers – Whole Numbers
An integer is a whole number — meaning it has no decimal or fractional part.
Examples of integers in JavaScript:
let a = 5;
let b = -10;
let c = 0;
JavaScript can safely represent integers between −(2^53 − 1) and 2^53 − 1 due to the limits of the IEEE 754 double-precision format.
For very large integers beyond this range, JavaScript provides the BigInt type.
Example:
let bigNumber = 123456789012345678901234567890n;
console.log(typeof bigNumber); // "bigint"
JavaScript Floats – Numbers with Decimals
A floating-point number (float) is any number that includes a decimal point.
Examples:
let pi = 3.14159;
let price = 99.99;
let temperature = -12.5;
Floats are ideal for calculations involving precision, but they can cause rounding issues because of how computers store decimal numbers.
Example of floating-point precision issue:
console.log(0.1 + 0.2); // 0.30000000000000004
This happens because of binary rounding limitations in floating-point arithmetic.
Checking if a Number is Integer or Float
1. Using Number.isInteger()
console.log(Number.isInteger(10));   // true
console.log(Number.isInteger(3.14)); // false
2. Checking for Floats
There’s no built-in isFloat() method, but you can check it using:
function isFloat(num) {
  return Number(num) === num && !Number.isInteger(num);
}
console.log(isFloat(3.14)); // true
console.log(isFloat(10));   // false
What is NaN in JavaScript?
NaN stands for “Not a Number.” It appears when you try to perform a mathematical operation that doesn’t result in a valid numeric value.
Examples:
console.log(Number("Hello")); // NaN
console.log(0 / 0);           // NaN
console.log(parseInt("abc")); // NaN
You can check if a value is NaN using:
console.log(isNaN("abc"));          // true
console.log(Number.isNaN(5 / "hi")); // true
💡
Number.isNaN()is preferred overisNaN()because it doesn’t coerce values before checking.
Converting NaN to Integer or Float in JavaScript
Sometimes, you’ll want to handle NaN values gracefully by converting them into valid integers or floats.
Here are several reliable methods.
1. Using parseInt() – Convert String or NaN to Integer
parseInt() parses a string and returns an integer. If it can’t parse a number, it returns NaN.
You can combine it with a fallback using logical OR (||) or nullish coalescing (??).
Example:
let value = parseInt("123"); // 123
let badValue = parseInt("abc") || 0; // fallback to 0
console.log(badValue); // 0
If the parsing fails (returns NaN), the fallback ensures you get a valid integer instead.
2. Using parseFloat() – Convert String or NaN to Float
parseFloat() works similarly but allows decimal points.
let price = parseFloat("45.67"); // 45.67
let wrong = parseFloat("abc") || 0.0;
console.log(wrong); // 0
3. Using Number() – Convert Anything to Number
Number() is a powerful function that converts any valid value into a number (integer or float).
When conversion fails, it returns NaN.
Example:
let num1 = Number("20");     // 20
let num2 = Number("3.14");   // 3.14
let num3 = Number("abc");    // NaN
You can handle NaN results using isNaN():
let result = Number("abc");
if (isNaN(result)) {
  result = 0; // default fallback
}
console.log(result); // 0
4. Using Math.floor(), Math.round(), or Math.trunc()
If you have a float and want to convert it to an integer:
let x = 7.9;
console.log(Math.floor(x)); // 7
console.log(Math.round(x)); // 8
console.log(Math.trunc(x)); // 7
These can be used after conversion from NaN as well:
let num = Number("abc"); // NaN
num = isNaN(num) ? 0 : Math.floor(num);
console.log(num); // 0
Converting Between Integers and Floats in JavaScript
Sometimes you may want to convert a float to an integer or vice versa.
Float to Integer
let floatNum = 42.89;
let intNum = parseInt(floatNum); // 42
Integer to Float
Simply add a decimal:
let intVal = 42;
let floatVal = intVal * 1.0; // 42 becomes 42.0
Or use division:
let floatNum = intVal / 1;
Handling NaN Gracefully in JavaScript
Instead of letting NaN break your calculations, you can handle it using:
function safeNumber(value, fallback = 0) {
  let num = Number(value);
  return isNaN(num) ? fallback : num;
}
console.log(safeNumber("abc")); // 0
console.log(safeNumber("45"));  // 45
console.log(safeNumber("3.14")); // 3.14
This ensures all inputs return valid numbers — great for form handling, APIs, and user input validation.
Best Practices for Working with Numbers in JavaScript
- Always validate user input before performing numeric operations.
 - Use 
Number.isNaN()instead ofisNaN(). - Use 
parseFloat()for decimals,parseInt()for integers. - Use fallback defaults to avoid NaN breaking your logic.
 - Be cautious of floating-point rounding errors in financial calculations.
 - For very large integers, use BigInt.
 
Frequently Asked Questions (FAQs)
1. Is there a difference between integer and float in JavaScript?
No — both are of type number. JavaScript uses double-precision floating-point format for all numeric values.
2. How can I check if a value is NaN?
Use Number.isNaN(value) to check if it’s NaN without type coercion.
3. How do I convert a NaN to 0 in JavaScript?
Use:
let value = Number("abc");
value = isNaN(value) ? 0 : value;
4. What’s the difference between parseInt() and Number()?
parseInt()reads until it encounters a non-numeric character.Number()must parse the entire string as a valid number.
Example:
parseInt("20px"); // 20
Number("20px");   // NaN
5. How can I convert a float to an integer without rounding?
Use Math.trunc() to remove the decimal part without rounding.
Math.trunc(9.99); // 9
Conclusion – Mastering Numbers, Integers, Floats, and NaN in JavaScript
JavaScript’s handling of numbers is both powerful and flexible. Whether you’re dealing with integers, floats, or unexpected NaN values, understanding how to convert and validate numbers correctly is essential for clean, bug-free code.
By using built-in methods like parseInt(), parseFloat(), and Number() — along with checks like Number.isNaN() — you can ensure your applications handle numbers accurately and safely.
