How to case-insensitively check if a character exists in a string with JavaScript

2 Answers

0 votes
const str = 'javascript c c++';
const ch = 'A';

console.log(str.toLowerCase().includes(ch.toLowerCase()));
console.log(str.includes('A')); 




/*
run:

true
false

*/

 



answered Feb 6, 2022 by avibootz
0 votes
// Case‑insensitive check using includes()
function charExistsIgnoreCase(s, target) {
    return s.toLowerCase().includes(target.toLowerCase());
}

// Define the string we want to search in
const s = "JavaScript";

// Perform the case-insensitive check
const exists = charExistsIgnoreCase(s, 'j');

// Print the raw boolean result
console.log(exists);

// Conditional check
if (exists) {
    console.log("exists");
} else {
    console.log("not exists");
}



/*
run:

true
exists

*/

 



answered 6 hours ago by avibootz
edited 5 hours ago by avibootz

Related questions

...