How do I check for vowels in JavaScript?

前端 未结 10 915
忘了有多久
忘了有多久 2020-12-01 02:55

I\'m supposed to write a function that takes a character (i.e. a string of length 1) and returns true if it is a vowel, false otherwise. I came up with two functions, but do

10条回答
  •  天命终不由人
    2020-12-01 03:24

    benchmark

    I think you can safely say a for loop is faster.

    I do admit that a regexp looks cleaner in terms of code. If it's a real bottleneck then use a for loop, otherwise stick with the regular expression for reasons of "elegance"

    If you want to go for simplicity then just use

    function isVowel(c) {
        return ['a', 'e', 'i', 'o', 'u'].indexOf(c.toLowerCase()) !== -1
    }
    

提交回复
热议问题