Detect if a String is in camelCase Format in Javascript or Node.js

Detecting if a JavaScript string value is in camelCase is likely an edge case. Yet, it can be a valid check in your codebase. This tutorial shows you how to determine whether a given string is in camelCase format.

Node.js Series Overview

Detect if a String Value is in camelCase

You should use the @supercharge/strings package to determine whether a given string is in camelCase format.

I’m the maintainer @supercharge/strings package which provides convenient string utilities. It comes a useful Str#isCamel() method detecting whether a given string value is in camelCase format:

const Str = require('@supercharge/strings')

Str('superchargeIsAwesome').isCamel()  
// true

Str('Supercharge').isCamel()  
// false

The implementation follows a basic idea: compare the given value against its camelCase’d version. The comparison returns true if both strings are identical and false otherwise.

Creating the camelCase version of a string value follows this idea:

  • replace symbols (like dashes and underscores) with a space
  • uppercase the first letter of each word
  • remove all spaces from the string
  • lowercase the first character

That’s it!


Mentioned Resources

Explore the Library

Find interesting tutorials and solutions for your problems.