Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9001785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:28:57+00:00 2026-06-16T00:28:57+00:00

Possible Duplicate: Javascript: The prettiest way to compare one value against multiple values Javascript

  • 0

Possible Duplicate:
Javascript: The prettiest way to compare one value against multiple values
Javascript If statement used to check file extensions not working

In JS I’m trying to check whether an extension ends in “png” “jpg” or “gif”. I’m aware this can be done with a switch statement, but I’m wondering if there’s a simpler way to throw it all in the if conditional. Like:

    if (aExtensions[i].toLowerCase() == ('jpg' || 'png' || 'gif')) {}

What’s the best way to achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:28:58+00:00Added an answer on June 16, 2026 at 12:28 am

    You could use an array like this:

    var extensions = ["jpg", "png", "gif"];
    
    if (extensions.indexOf(aExtensions[i].toLowerCase()) > -1) {
        // match
    }
    

    In this case, you store the “valid” extensions. Then, you can use the indexOf method of Array to find if any of the items in the array match the specific extension you’re looking at – checking for a value that is 0 or greater.

    indexOf isn’t supported on older browsers, so you’d need to include a polyfill to back it up. There are several solutions for that.

    Of course, if you wanted to only use if statements, you could use this format:

    var ext = aExtensions[i].toLowerCase();
    if (ext == "jpg" || ext == "png" || ext == "gif") {
        // match
    }
    

    Another possibility I can think of is a switch statement, like:

    var ext = aExtensions[i].toLowerCase();
    
    switch (ext) {
        case "jpg":
        case "png":
        case "gif":
            // match
            break;
        case default:
            // No match
    }
    

    I might as well include it (other answers had it first, definitely), but one more I can think of is using a regex, like:

    if (/jpg|png|gif/i.test(aExtensions[i])) {
        // match
    }
    

    But note that you will never be able to individually get the extensions back, and that’s why I would prefer one of the first two options I gave.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: JavaScript: formatting number with exactly two decimals Can some one please help
Possible Duplicate: javascript - check if string begins with something? I've read in this
Possible Duplicate: Javascript color gradient I have color one (let's say yellow) and color
Possible Duplicate: JavaScript equivalent to printf/string.format How can I create a Zerofilled value using
Possible Duplicate: JavaScript: How do I print multiple script tags in a loop? Im
Possible Duplicate: Javascript AES encryption Is there a way to validate an AES encrypted
Possible Duplicate: javascript domready? I want to check whether $(function(){ }); is ready. Return
Possible Duplicate: Javascript hard refresh of current page Is there a way to refresh
Possible Duplicate: Javascript or Jquery to check and uncheck all checkbox I have 12
Possible Duplicate: JavaScript Debugger Is there any way to debug javascript. I found many

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.