Leaderboard
Javascript Jun 12, 2026
Javascript String Includes Case Sensitive

JavaScript String.includes() — What does this output?

This is a daily Javascript challenge from the CodeShot archive. Practice your knowledge of String Includes Case Sensitive and improve your technical interview readiness.

const text = "Hello, World!"
console.log(text.includes("World"))
console.log(text.includes("world"))
A true and true
B true and false
C false and false
D false and true

Detailed Explanation

Why This Question Matters

If you're just starting with JavaScript, you'll likely run into this specific behavior within your first week. It seems straightforward: you're just checking if a string contains a certain word, right? But this is where a lot of juniors get tripped up.

The confusion stems from a fundamental rule in almost every programming language: case sensitivity. In the real world, "World" and "world" are the same word. In JavaScript, they are completely different sets of data. If you don't account for this, you'll end up with bugs where a user types their email in uppercase, and your validation logic suddenly decides they aren't a registered user.

Understanding the Code

Let's look at the snippet:

const text = "Hello, World!"
console.log(text.includes("World"))
console.log(text.includes("world"))

Here, we have a string stored in the constant text. We're using the .includes() method, which was introduced in ES6.

Internally, .includes() does exactly what it sounds like. It scans the string from left to right and checks if the sequence of characters provided in the argument exists exactly as written. It returns a boolean: true if it finds a match, and false if it doesn't.

The first call text.includes("World") looks for a capital 'W' followed by 'orld'. Since our string is "Hello, World!", it finds a perfect match.

The second call text.includes("world") looks for a lowercase 'w'. Even though the letters are the same, the character codes are different. In the ASCII/Unicode table, 'W' and 'w' have different numeric values. JavaScript doesn't "guess" that you mean the same word; it just sees that the characters don't match and returns false.

Finding the Correct Answer

The output of this code is:
true
false

This makes Option B the correct answer.

If you thought the answer would be true, true, you're thinking like a human reading a sentence. But the engine is thinking like a machine comparing bits. Because the second check uses a lowercase "w" and the original string uses an uppercase "W", the match fails.

Common Mistakes Developers Make

The biggest mistake is assuming that JavaScript handles "fuzzy matching" by default. It doesn't.

Another common slip-up is forgetting that .includes() is case-sensitive across the board. Beginners often try to "fix" this by writing multiple if statements to check for every possible capitalization (e.g., checking for "World", "WORLD", and "world"), which is a nightmare to maintain.

There's also a tendency to confuse .includes() with .indexOf(). While .indexOf() is the older way of doing this, it returns the index number (or -1 if not found). Using .includes() is much cleaner for simple true/false checks, but the case-sensitivity rule applies to both.

Real-World Usage

In a production environment, you'll rarely trust user input to be perfectly capitalized. Imagine you're building a search bar for a blog. If a user searches for "javascript", but your article title is "JavaScript Tutorial", a simple .includes() check will return false, and the user will think your site is broken.

To handle this, the industry standard is to normalize your strings. You convert both the source string and the search term to the same case (usually lowercase) before comparing them.

Here is how you'd actually write this in a real app:

const text = "Hello, World!";
const searchTerm = "world";

// Normalize both to lowercase to ensure a match regardless of case
const isMatch = text.toLowerCase().includes(searchTerm.toLowerCase());

console.log(isMatch); // true

By calling .toLowerCase() on both sides, you eliminate the case-sensitivity problem entirely. This is the "pro" way to handle string searches, filters, and basic validation.

Key Takeaways

- JavaScript is case-sensitive. "A" is not "a".
- .includes() checks for an exact character match and returns a boolean.
- Don't trust user input. Users will type in mixed cases, and your code needs to handle that.
- Normalize your data. Use .toLowerCase() or .toUpperCase() when you want to perform a case-insensitive search.

Why this matters

Understanding String Includes Case Sensitive is crucial for passing technical interviews. In real-world applications, this concept often leads to subtle bugs if not handled correctly. For more details, you can always refer to the official MDN Documentation.

📝
Reviewed by CodeShot Editorial
Every challenge is code-reviewed by senior developers to ensure accuracy and real-world relevance. Learn more.

Ready for your shot?

Join thousands of developers solving one logic puzzle every morning.

Solve Today's Challenge →