Check If a String Is in Lowercase in JavaScript or Node.js

The JavaScript stdlib is missing a method to detect whether a given input is a string value in lowercase format. Yet, we can build our own isLowerCase method using the existing methods.

This tutorial shows you how to create a method detecting whether an input is in lowercase.

Node.js Series Overview

Determine Whether a JavaScript String is in Lowercase

You can detect whether a given input is a lowercase string in JavaScript using a comparison of the given value against its lowercased pendant. JavaScript provides a toLowerCase method on string values. That’s what we’ll use for our isLowerCase method.

You can ensure a string value by wrapping a given input using the globally available String(input) function. This is useful because we can call toLowerCase only on string inputs.

Here’s a sample utility function determining whether a given input is a lowercased string:

 * Determine whether the given `input` is a string in lowercase.
 * @param {*} input
 * @returns {Boolean}
function isLowerCase (input) {  
  return input === String(input).toLowerCase()

You may then use the isLowerCase utility function like this:

isLowerCase('future studio')  
// true

isLowerCase('Future Studio')  
// false

// false

// false

Please notice, JavaScript doesn’t treat null or undefined as “nullish” values when creating a string of them using the String() function. It creates a string representation of the given value:

// ''

// 'null'

// 'undefined'

This can lead to unexpected behavior in your application when working with nullish values. Depending on your expected behavior you may need to add checks for undefined and null values to the isLowerCase utility from above.

Use the @supercharge/strings Package

I’m the maintainer of the @supercharge/strings package providing convenient string utilities. It provides a useful Str#isLower method determining whether a given string is in lowercase:

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

Str('future studio').isLower()  
// true

Str('Future Studio').isLower()  
// false

// true

// true

The @supercharge/strings package treats undefined and null values as empty strings. That’s the consistent behavior you want in your app 🤝

Mentioned Resources

Explore the Library

Find interesting tutorials and solutions for your problems.