How to remove decimals in JavaScript?

In this article, we will show you the different methods and techniques on how to remove decimals in JavaScript.

Also, we will provide you with a complete guide to take on this common programming challenge.

In web development, JavaScript is a dynamic and functional programming language.

It offers a vast range of functionalities, including the ability to manipulate numbers and perform calculations.

However, when working with decimal numbers, you may encounter situations where you need to remove the decimal part and work with whole numbers only.

Methods to Remove Decimals in JavaScript

This is the following methods to remove decimals in JavaScript.

Method 1: Using the parseInt() Function

One of the simple method to remove decimals in JavaScript is by using the parseInt() function.

This function parses a string and returns an integer value, excluding any decimal places.

Here’s an example code of how to use the parseInt() function:

let decimalNumberExample = 20.5;
let wholeNumberExample = parseInt(decimalNumberExample);
console.log(wholeNumberExample);

By applying parseInt() to a decimal number, we forcefully remove the decimal part and gain the whole number.

However, it is important to consider that this method only works for positive numbers.

If you have negative decimal numbers, you might encounter unexpected results.

Method 2: Using the Math.floor() Function

Another frequently used method to remove decimals in JavaScript is by utilizing the Math.floor() function.

This function rounds a number descending to the nearest integer.

Here’s an example code of how to use the Math.floor() function:

let decimalNumberExample = 20.5;
let wholeNumberExample = Math.floor(decimalNumberExample);
console.log(wholeNumberExample);

The Math.floor() function forcefully truncates the decimal part of the number, returning the largest integer less than or equal to the given value.

This method is useful when you want to secure that the decimal part is always eliminated, regardless of whether the number is positive or negative.

Method 3: Using the Math.trunc() Function

If you definitely want to remove the decimal part without rounding the number, you can use the Math.trunc() function.

This function commonly truncates the decimal part of the number, leaving only the integer component.

Here’s an example code of how to use the Math.trunc() function:

let decimalNumberExample = 20.5;
let wholeNumberExample = Math.trunc(decimalNumberExample);
console.log(wholeNumberExample);

By applying Math.trunc() function to a decimal number, we can completely remove the decimal part without any rounding.

This method is commonly useful when you need to keep the original integer value without any modification.

Method 4: Using the toFixed() and parseFloat() Functions

In some scenario, you might want to eliminate the decimal part and convert the number to a string representation without any decimal places.

One method to done this is by combining the toFixed() and parseFloat() functions.

Here’s an example code of how to use the toFixed() and parseFloat() functions:

let decimalSampleNumber = 23.3333;
let SamplewholeNumber = parseFloat(decimalSampleNumber.toFixed(0));
console.log(SamplewholeNumber); 

The toFixed() function is used to defined the number of decimal places to keep.

In this example code, we pass 0 as an argument, showing that we want to remove all decimal places.

Then, the parseFloat() function is applied to convert the resulting string back to a number, effectively removing the decimals.

Method 5: Using Bitwise Operators

For advanced JavaScript developers, another interesting method to remove decimals is by using bitwise operators.

The bitwise shift operators can be used to truncate the decimal part of a number by converting it to a 32-bit integer.

Here’s an example code that use the bitwise operators:

let decimalSampleNumber = 23.3333;
let SamplewholeNumber = decimalSampleNumber | 0;
console.log(SamplewholeNumber); 

By using the bitwise OR operator (|) with 0, we effectively convert the number to a 32-bit integer, removing the decimal part.

However, it’s important to know that this method only works for numbers within the range of a 32-bit integer, which is approximately -2 billion to 2 billion.

FAQs

How can I remove decimals in JavaScript without rounding the number?

To remove decimals without rounding the number, you can use the Math.trunc() function. It truncates the decimal part, leaving only the integer component.

What’s the difference between parseInt() and Math.floor()?

The parseInt() function converts a string to an integer by removing any decimal places, whereas Math.floor() rounds a number downwards to the nearest integer, discarding the decimal part.

Can I remove decimals from negative numbers?

Yes, you can remove decimals from negative numbers using methods such as Math.floor() and Math.trunc(). These methods work regardless of the sign of the number.

Are there any limitations to using bitwise operators for removing decimals?

Yes, when using bitwise operators, the number should be within the range of a 32-bit integer. Numbers outside this range may result in unexpected behavior.

Conclusion

In this article, we have discussed the different methods to remove decimals, including parseInt(), Math.floor(), Math.trunc(), and combining toFixed() with parseFloat().

Each method has its own advantages and use cases, so choose the one that best fits your requirements.

Additional Resources

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Leave a Comment