• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Basics / The JavaScript function Math.floor()

The JavaScript function Math.floor()

The JavaScript function Math.floor()

The JavaScript function Math.floor() is used to round a number down to the nearest integer. This function takes a single argument, which is the number that you want to round down.

For example, if you wanted to round the number 4.7 down to the nearest integer, you would use the following code:

let number = 4.7;
let rounded = Math.floor(number);
console.log(rounded); // Output: 4

As you can see, the Math.floor() function is called with the variable number as the argument, and the result is stored in the variable rounded. When you run this code, the output will be 4, which is the nearest integer less than 4.7.

You can also use Math.floor() to round down negative numbers. For example:

let negativeNumber = -4.7;
let rounded = Math.floor(negativeNumber);
console.log(rounded); // Output: -5

In this example, the number -4.7 is passed to the Math.floor() function, and the result is -5, which is the nearest integer less than -4.7.

Additionally, you can use the Math.floor() function with expressions, for example:

let number = 5 / 2;
let rounded = Math.floor(number);
console.log(rounded); // Output: 2

The Math.floor() function is a useful tool for working with numbers in JavaScript, and it can be used in a variety of situations where you need to round a number down to the nearest integer.

Basics, Math Related

Related Snippets:

  • Check if the given number is even
  • Generating random numbers with JavaScript
  • Take a number and return it in the specified currency formatting
  • One-Liner If-Else Statements

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers