JavaScript Math.clz32() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

Math.clz32(x) counts the leading zero bits when a number is written as a 32-bit unsigned integer. The name means count leading zeros, 32-bit.

Math is built into JavaScript. Call the method directly. It is not a constructor, so never write new Math().

Syntax

Call Math.clz32() like this. Start with this line.

JavaScript
Math.clz32(x)
CallWhat it does
Math.clz32(x)Returns the number of leading zero bits in the 32-bit form of x.

Built into JavaScript. Call Math.clz32() on the Math object — no import required.

Library declarations
JavaScript
Math.clz32(x)
JavaScript
interface Math {
  clz32(x: number): number;
}

Parameters

ParameterDescription
xRequired. A number. It is converted to an unsigned 32-bit integer first.

Return Value

Return ValueDescription
numberA whole number from 0 to 32: the count of leading zero bits.

JavaScript Examples

Example 1 — Basic Use

A bigger number has fewer leading zeros. Binary literals start with 0b.

JavaScript
console.log(Math.clz32(1));        // 31
console.log(Math.clz32(0b1000));   // 28
console.log(Math.clz32(255));      // 24
console.log(Math.clz32(0));        // 32
Try It Yourself

How It Works

  1. One. 1 is a single bit at the end, so 31 zeros come first.
  2. Binary literal. 0b1000 is 8. It uses 4 bits, so 28 leading zeros.
  3. 255. It needs 8 bits (11111111), leaving 24 zeros.
  4. Zero. No 1 bit at all, so the answer is 32.

Example 2 — Bit Length of a Number

Subtract the leading zeros from 32 to see how many bits a number needs.

JavaScript
function bitLength(n) {
  return 32 - Math.clz32(n);
}

console.log(bitLength(1));    // 1
console.log(bitLength(255));  // 8
console.log(bitLength(256));  // 9
Try It Yourself

How It Works

  1. 32 bits total. A 32-bit number has 32 bit slots.
  2. Remove zeros. Subtract the leading zeros to count the used bits.
  3. Edge. 255 needs 8 bits. 256 needs one more bit, so 9.

Example 3 — Special Values and Floor of log2

Decimals are cut, NaN and Infinity act like 0. You can also get the floor of log2 quickly.

JavaScript
console.log(Math.clz32(3.9));       // 30
console.log(Math.clz32(NaN));       // 32
console.log(Math.clz32(Infinity));  // 32
console.log(Math.clz32(-1));        // 0

console.log(31 - Math.clz32(1000)); // 9
console.log(Math.floor(Math.log2(1000)));  // 9
Try It Yourself

How It Works

  1. Decimals. 3.9 becomes 3 (binary 11), so 30 zeros.
  2. NaN and Infinity. Both become 0, so the answer is 32.
  3. Negative. -1 is all ones in 32 bits, so there are 0 zeros.
  4. Fast log2. 31 - Math.clz32(n) equals Math.floor(Math.log2(n)) for positive integers.

Browser & Runtime Support

Math.clz32() is part of ES2015 and is Baseline Widely available in modern browsers and Node.js.

✓ Baseline · ES2015

Math.clz32()

Supported in Chrome, Firefox, Safari, Edge, and Node.js for years. Safe for modern apps without a polyfill.

98% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Math.clz32 Excellent

Bottom line: Use Math.clz32() in new code. Only very old browsers, like Internet Explorer, lack it.

Frequently Asked Questions

Math.clz32(x) counts how many zero bits come before the first 1 bit in the 32-bit form of x. Math.clz32(1) is 31.
It stands for "count leading zeros, 32-bit".
It returns 32, because all 32 bits are zeros.
Use 32 - Math.clz32(n). For example, 255 needs 32 - 24 = 8 bits.
It was added in ES2015 and works in all modern browsers and Node.js.

Next: cos()

Learn how to find the cosine of an angle.

cos() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful