JavaScript: JavaScript Utility Methods

Last updated: 2026-10-13

We've covered type conversions — now let's learn a batch of utility methods you'll use every day. Think of them as a Swiss army knife — each one is simple, but you'll miss any one of them when you need it.

Related Array Methods:Array.isArray() Number.isNaN() Math.trunc parseFloat Number() toString parseInt toFixed typeof array

1. Essential Utility Methods

(1) parseInt and parseFloat

Parse numbers from the beginning of a string, stopping at the first non-numeric character:

HTML
<script>
console.log(parseInt("42px"));      // 42
console.log(parseInt("3.14"));      // 3 (integer part only)
console.log(parseFloat("3.14"));    // 3.14
console.log(parseFloat("3.14abc")); // 3.14
console.log(parseInt("abc42"));     // NaN (doesn't start with a number)
</script>

The second argument of parseInt specifies the radix:

HTML
<script>
console.log(parseInt("FF", 16));  // 255 (hexadecimal)
console.log(parseInt("10", 2));   // 2 (binary)
</script>

Pitfall: Without a radix, strings starting with 0x are parsed as hexadecimal. Always pass 10 as the second argument.

(2) isNaN vs Number.isNaN

Method Behavior
isNaN(value) Converts to number first, then checks if it's NaN
Number.isNaN(value) Strictly checks if the value is NaN (no conversion)
HTML
<script>
console.log(isNaN("hello"));         // true (Number("hello") → NaN, then true)
console.log(Number.isNaN("hello"));  // false ("hello" itself is not NaN)
console.log(isNaN(undefined));       // true
console.log(Number.isNaN(undefined));// false
</script>

Conclusion: Use Number.isNaN() — it won't silently coerce types.

(3) toFixed

Controls decimal places, returns a string:

HTML
<script>
console.log((3.14159).toFixed(2));  // "3.14"
console.log((3.1).toFixed(4));      // "3.1000" (padded with zeros)
console.log((3.145).toFixed(2));    // "3.14" or "3.15" (floating-point precision issue, rounding not guaranteed)
</script>

Note that toFixed returns a string — use Number() to convert it back for calculations.

(4) toString

Converts a number to a string, with optional radix support:

HTML
<script>
console.log((255).toString());      // "255"
console.log((255).toString(16));    // "ff"
console.log((8).toString(2));       // "1000"
console.log((100).toString(8));     // "144"
</script>

Booleans and arrays also have toString:

HTML
<script>
console.log(true.toString());       // "true"
console.log([1,2,3].toString());    // "1,2,3"
</script>

(5) Number.isInteger

Checks whether a value is an integer:

HTML
<script>
console.log(Number.isInteger(5));      // true
console.log(Number.isInteger(5.0));    // true (5.0 is just 5)
console.log(Number.isInteger(5.5));    // false
console.log(Number.isInteger("5"));    // false (a string is not an integer)
console.log(Number.isInteger(NaN));    // false
</script>

(6) Math.trunc and Math.sign

Math.trunc simply chops off the decimal part (no rounding):

HTML
<script>
console.log(Math.trunc(4.9));    // 4
console.log(Math.trunc(-4.9));   // -4
console.log(Math.trunc(4.1));    // 4
</script>

Math.sign returns the sign of a number:

HTML
<script>
console.log(Math.sign(5));       // 1
console.log(Math.sign(-5));      // -1
console.log(Math.sign(0));       // 0
console.log(Math.sign(-0));      // -0
console.log(Math.sign(NaN));     // NaN
</script>

(7) Limitations of typeof

typeof usually works for type checking, but there are a few classic gotchas:

HTML
<script>
console.log(typeof 42);           // "number"  ✅
console.log(typeof "hello");      // "string"  ✅
console.log(typeof true);         // "boolean" ✅
console.log(typeof undefined);    // "undefined" ✅
console.log(typeof null);         // "object"  ❌ This is a historical bug!
console.log(typeof []);           // "object"  ❌ Arrays are also objects
console.log(typeof {});           // "object"  ✅
console.log(typeof function(){}); // "function" ✅
</script>

typeof null === "object" is a "veteran bug" in JavaScript — it's been around since 1995 and can never be fixed. Use value === null to check for null. Use Array.isArray() to check for arrays.


▶ Example: Price Calculator

▶ Try it Yourself

▶ Example: Input Validation Tool

▶ Try it Yourself

▶ Example: typeof and Type Checking

▶ Try it Yourself

Related Array Methods:Array.isArray() Number.isNaN() Math.trunc parseFloat Number() toString parseInt toFixed typeof array

❓ FAQ

Q Why doesn't toFixed guarantee rounding?
A Because floating-point numbers can't be represented exactly in binary. (0.1 + 0.2).toFixed(1) might give "0.3" or might not. For precise rounding, use Math.round(num * 100) / 100 before calling toFixed.
Q What's the difference between parseFloat and Number()?
A parseFloat("42px") returns 42 (parses from the start, stops at the first non-numeric character), while Number("42px") returns NaN (parses the whole string — any non-numeric character causes failure). Use parseFloat to extract numbers from mixed text; use Number() for strict validation.

📖 Summary

📝 Exercises

  1. Write a function parseChineseNumber(str) that parses strings like "3.5🦄" → 3.5, "100🦄" → 100, "🦄200" → 200 (using parseFloat). Handle the case where plain text returns NaN.
  2. Write a function formatMoney(amount) that takes a number and returns a string with comma-separated thousands and two decimal places, e.g. 1234567.89 → "1,234,567.89" (hint: use toFixed and toString with array methods).
  3. Write a function getType(value)🦄 typeof 🦄:🦄 null 🦄 null,🦄 array,🦄 typeof 🦄。
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏