Sling Academy
Home/JavaScript/JavaScript: Ways to Compare 2 Strings Ignoring Case

JavaScript: Ways to Compare 2 Strings Ignoring Case

Last updated: February 28, 2023

This succinct, example-based article walks you through 3 different ways to compare 2 strings ignoring cases (regardless of lowercase and uppercase, for example, A and a are considered equal) in JavaScript.

Using toLowerCase() or toUpperCase() and the === operator

The technique is intuitive and easy to understand. First, we convert both strings to lowercase or uppercase using the toLowerCase() or toUpperCase(). Next, we use the === operator to compare them.

Example:

const str1 = 'Sling Academy';
const str2 = 'sling academy';

if (str1.toLowerCase() === str2.toLowerCase()) {
  console.log('Strings are equal ignoring case.');
} else {
  console.log('Strings are not equal.');
}

Output:

Strings are equal ignoring case.

Using localeCompare()

Another solution is to use the localeCompare() method with the ignoreCase parameter set to true. This method returns 0 if both strings are equal, regardless of their case.

Example:

const str1 = 'WELCOME TO SLING ACADEMY';
const str2 = 'Welcome to sling academy';

const result = str1.localeCompare(str2, undefined, { sensitivity: 'base' });

if (result === 0) {
  console.log('Strings are equal ignoring case.');
} else {
  console.log('Strings are not equal.');
}

Output:

Strings are equal ignoring case.

Using regular expressions

If a regular expression is created using the RegExp() constructor with the i flag, which means it is case-insensitive.

Example:

const str1 = 'The fire is getting bigger and bigger.';
const str2 = 'THE FIRE IS GETTING BIGGER AND BIGGER.';

const regex = new RegExp(str1, 'i');

if (regex.test(str2)) {
  console.log('Strings are equal ignoring case.');
} else {
  console.log('Strings are not equal.');
}

Output:

Strings are equal ignoring case.

The test() method is used to check if the second string matches the regular expression created from the first string, ignoring case.

Next Article: JavaScript: Convert a string to Unicode code points (2 ways)

Previous Article: JavaScript: 2 Ways to Remove HTML Tags from a String

Series: JavaScript Strings

JavaScript

You May Also Like

  • Handle Zoom and Scroll with the Visual Viewport API in JavaScript
  • Improve Security Posture Using JavaScript Trusted Types
  • Allow Seamless Device Switching Using JavaScript Remote Playback
  • Update Content Proactively with the JavaScript Push API
  • Simplify Tooltip and Dropdown Creation via JavaScript Popover API
  • Improve User Experience Through Performance Metrics in JavaScript
  • Coordinate Workers Using Channel Messaging in JavaScript
  • Exchange Data Between Iframes Using Channel Messaging in JavaScript
  • Manipulating Time Zones in JavaScript Without Libraries
  • Solving Simple Algebraic Equations Using JavaScript Math Functions
  • Emulating Traditional OOP Constructs with JavaScript Classes
  • Smoothing Out User Flows: Focus Management Techniques in JavaScript
  • Creating Dynamic Timers and Counters with JavaScript
  • Implement Old-School Data Fetching Using JavaScript XMLHttpRequest
  • Load Dynamic Content Without Reloading via XMLHttpRequest in JavaScript
  • Manage Error Handling and Timeouts Using XMLHttpRequest in JavaScript
  • Handle XML and JSON Responses via JavaScript XMLHttpRequest
  • Make AJAX Requests with XMLHttpRequest in JavaScript
  • Customize Subtitle Styling Using JavaScript WebVTT Integration