Why this JavaScript doesn't work?

var hostName = window.location.hostname; // Get the full hostname of the current URL
var topLevelDomain = hostName.substring(hostName.lastIndexOf('.') + 1); // Extract the top-level domain (e.g. "com", "org", "edu", etc.)

if (topLevelDomain === 'com') {
    // Top-level domain is ".com", set variable to "en"

It should detect a top level domain and save “en” to a string if a site has “.com”