Dimitris Xydas Dimitris Xydas - 3 months ago 20
Javascript Question

Case sensitivity of argument?

Is there a way to pass case sensitivity together with an argument?

var user = {
'name' : 'Jim',
'lastName' : 'Xydas',
'fullName' : function(){
return this.name + " " + this.lastName
},

'address' : {
'country' : "Greece",
'town' : "Thessaloniki",
'street' : "il.ap 15",

"isFromTown" : function(n){
return this.town == n ? true : false;
}
}
};

var checkUser = user.address.isFromTown("thessaloniki");

console.log(checkUser);


What I'm trying to do here is pass the argument as "thessaloniki" and print true, ignoring the first (or more) uppercase letter(s).

https://jsfiddle.net/DimitriXd4/mfpat7so/

Thanks in advance.

Answer

You could use String#toLowerCase() or String#toLocaleLowerCase for both strings for compairing.

var user = {
    'name' : 'Jim',
    'lastName' : 'Xydas',
    'fullName' : function() {
        return this.name + " " + this.lastName
     },
    'adress' : {
        'country' : "Greece",
        'town'    : "Thessaloniki",
        'street'  : "il.ap 15",
        "isFromTown" : function(n){
            return this.town.toLowerCase() == n.toLowerCase();
        }
    }
};

var checkUser = user.adress.isFromTown("thessaloniki");

console.log(checkUser);