# JavaScript Math Snippets

The JavaScript snippet collection contains a wide variety of ES6 helper functions. It includes helpers for dealing with primitives, arrays and objects, as well as algorithms, DOM manipulation functions and Node.js utilities.

• JavaScript ·

### Calculate the angle between two vectors in JavaScript

Learn how to calculate the angle (theta) between two vectors in JavaScript.

• JavaScript ·

### Pretty-print number of bytes

Converts a number in bytes to a human-readable string.

• JavaScript ·

### Fibonacci

Generates an array, containing the Fibonacci sequence, up until the nth term.

• JavaScript ·

### Generate a JavaScript array of random integers in a given range

Combining `Math.random()` and a few simple math operations, you can generate an array of random integers in a specified range.

• JavaScript ·

### Find the prime factors of a number in JavaScript

Find the prime factors of a number using trial division in JavaScript.

• JavaScript ·

### Percentile of matches

Calculates the percentage of numbers in the given array that are less or equal to the given value.

• JavaScript ·

### Clamp or map a number to a range in JavaScript

Clamping and mapping a number to a range are two common and easily confused operations. Learn how to perform each in JavaScript.

• JavaScript ·

### Convert decimal number to hexadecimal

Ever needed to convert a decimal number to hexadecimal? Here's a quick and easy way to do it.

• JavaScript ·

### Generate a random number or integer in a range with JavaScript

Enhance JavaScript's `Math.random()` to generate a random number or integer in a specified range.

• JavaScript ·

### Hamming distance

Calculates the Hamming distance between two values.

• JavaScript ·

### Validate a number in JavaScript

Check if a value can be safely converted to a number in JavaScript.

• JavaScript ·

### Calculate the quotient and remainder of a division in JavaScript

Implement Python's `divmod()` built-in function in one line of JavaScript.

• JavaScript ·

### JavaScript Logical Operations

Get started with logical operations in JavaScript with this collection of helper functions.

• JavaScript ·

### JavaScript's numeric separators explained

Numeric separators are a somewhat lesser-known JavaScript syntactic sugar that can make working with large constants a lot easier.

• JavaScript ·

### Generate a random boolean value in JavaScript

A quick, one-liner to generate a random boolean value in JavaScript.

• JavaScript ·

### Binomial coefficient

Calculates the number of ways to choose `k` items from `n` items without repetition and without order.

• JavaScript ·

### Factorial of number

Calculates the factorial of a number.

• JavaScript ·

### How can I check if a number is even or odd using JavaScript?

Check if a number is even or odd using JavaScript using the modulo operator or bitwise AND operator.

• JavaScript ·

### What is the difference between prefix and postfix operators?

While both the prefix and postfix operators increment a value, the resulting value of the expression is very different.

• JavaScript ·

### Copy sign from one number to another in JavaScript

Copy the sign of one number to another without changing its absolute value.

• JavaScript ·

### Number in range

Checks if the given number falls within the given range.

• JavaScript ·

### How can I specify the base for a logarithm in JavaScript?

Calculate the logarithm of a number or check if a number is a power of a specific base.

• JavaScript ·

### Check if two numbers are approximately equal in JavaScript

Learn how to check if two numbers are approximately equal to each other in JavaScript.

• JavaScript ·

### Calculate the nth root of a number in JavaScript

Use `Math.pow()` to calculate the nth root of a given number in JavaScript.

Start typing a keyphrase to see matching snippets.