6

If i had an array of days names and i wanted to check for example if sunday - first letter capital or small - in this array what would be the best thing to do ?

1

2 Answers 2

22

You may also use Array.indexOf:

var days = ["monday",
            "tuesday",
            "wednesday",
            "thursday",
            "friday",
            "saturday",
            "sunday"];

function isInArray(days, day) {
    return days.indexOf(day.toLowerCase()) > -1;
}

isInArray(days, "Sunday");  // true
isInArray(days, "sunday");  // true
isInArray(days, "sUnDaY");  // true
isInArray(days, "Anyday");  // false

Check the browser compatibility in MDN.

Sign up to request clarification or add additional context in comments.

Comments

4
function is_in_array(s,your_array) {
    for (var i = 0; i < your_array.length; i++) {
        if (your_array[i].toLowerCase() === s.toLowerCase()) return true;
    }
    return false;
}

Usage:

var arr = ["hello","ToTo"];
is_in_array("toto",arr) //true
is_in_array("todto",arr) //false

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.