7 JavaScript Utility Functions to Improve Your Efficiency

Detect browser, detect function type, convert hyphen-case to camelCase, delete HTML tag in a string and reverse a string, etc.

bitfish
bitfish
Feb 13 · 3 min read
Photo by 🇨🇭 Claudio Schwarz | @purzlbaum on Unsplash

Detect Browser

Different browsers have different navigator.useragent. We can detect browser types by checking this value.

Example:

Chrome on Mac OS

There was a 'chrome' string on the result.

Firefox on Mac OS

There was a 'firefox' string on the result.

So we just need to check whether there is a browser brand name in navigator.useragent, and then we can detect the browser type.

Detect Function Type

there were two types of functions:

  • native functions provided by the runtime environment. For example Array.isArray , console.log .
  • functions that were written by users

In some complex cases, you may need to distinguish between these two types of functions.

So how do we distinguish these two types of functions in our code? Very simply, they have different results when converted to strings.

Native Functions
user-written function

When we convert a native function to a string, the result will always contain native code .

So we can write this function:

Convert hyp-hens to camelCase

Converting hello-world style strings to helloWorld style strings is a very common requirement. To do this, we can use regular expressions.

We can use /-(\w)/gto match all lowercase letters after and then use its upper case to replace it.

Delete HTML tag in string

For security reasons, we often need to remove HTML tags from a string. With a simple regular expression, we can easily accomplish this task.

const stripHTMLTags = str => str.replace(/<[^>]*>/g, '');

Reverse a String

Reverse a string is a common requirement. To do that, we can split a string to an array then reverse the array and join it.

const stripHTMLTags = str => str.replace(/<[^>]*>/g, '');

Format a number to a string with comma

In order to make a large number easier to read, we often add some separator in the middle of the number.

  • 111111 => 111,111
  • 123456789 => 123,456,789

Usually, we add a comma to every three numbers.

Convert byte units to reasonable units

Inside a computer, the size of a file is often measured in bytes. But if it’s a large number, it’s not readable for humans.

For example, 98223445B, when we first see this number, it is difficult to have any intuitive feelings. But if we use 93.7 MB, we are very familiar with it. So we need to write a function, which is very convenient for us to accomplish this goal

JavaScript in Plain English

Learn the web's most important programming language.

bitfish

Written by

bitfish

Love reading, writing and programming.

JavaScript in Plain English

Learn the web's most important programming language.

More From Medium

More from JavaScript in Plain English

More from JavaScript in Plain English

More from JavaScript in Plain English

32 funny Code Comments that people actually wrote

10.4K

More from JavaScript in Plain English

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade