Java script file extensions
JavaScript Array lastIndexOf. JavaScript Program to Get File Extension In this example, you will learn to write a JavaScript program that will get the file extension of a given filename. The filename is split into individual array elements using the split method. Here, filename. The last array element, which is the extension, is returned using the pop method. Share on:. Did you find this article helpful?
Sorry about that. How can we improve it? Isn't it expensive to exec the regex twice? The highly-rated answer below is much better. Unfortunately both solutions fail for names like file and. All possible cases are processed as follows: return filename. JustAndrei Still not all : For pure file name basename?
That is if you want to keep it as a one-liner, which is a bit messy to my taste. Show 8 more comments. Anye 14 14 bronze badges. I can't comment on the performance, but this one certainly looks clean!
I'm using it. Kindly consider the output. I hope it will be false. Thanks very much. It's nice to see a solution not using regex; I have done this with PHP and it only uses a couple of functions.
Wouldn't this simply return the base filename, which would be kinda bad? Show 14 more comments. The following solution is fast and short enough to use in bulk operations and save extra bytes: return fname.
If the substring is not found method returns The "unacceptable" positions of dot in the filename are -1 and 0 , which respectively refer to names with no extension e. If the position number is more than the length of the string method returns "". PedroZorus 4 4 silver badges 16 16 bronze badges. VisioN VisioN k 29 29 gold badges silver badges bronze badges. Read question carefully before downvoting. Why go to lengths to optimize such a trivial line of code? Tilde and bitshift operators are so seldom seen in JavaScript that I cannot support such an answer.
If it takes 5 bullet points to explain how 1 line of code works, better to rewrite the code so it is actually understandable. The speed of this one line will make no perceptible difference in any application. Obsessing over the performance and compactness of this logic has degraded the quality of the code; if code requires "extra investigation," I consider it "bad.
Jackson Considering this is a site for offering multiple solutions to a problem, having a solution which optimizes performance is never a bad thing.
You're statement "will make no perceptible difference in any application" is completely based on your narrow scope of possible applications this could be used in. Beyond that, it may provide someone looking at the problem a learning experience in which they can optimize some other code they need to make for a computing intensive application they are writing.
Show 17 more comments. PhiLho PhiLho Dima Dima 4 4 silver badges 7 7 bronze badges. There is a standard library function for this in the path module: import path from 'path'; console. There is no reason to do string splitting or regex. Who said anything about node? Your argument for not using splitting or regular expressions is to include plugin or bundle the application with node, it's an over the top answer for a menial task — Shannon Hochkins.
ShannonHochkins most of the time you have these things set up anyway — sdgfsdh. AndreiDraganescu I didn't intend it to be condescending, so have toned it down. Marketplace - ESLint. Publisher - Dirk Baeumer. Easily integrate ESLint into your project. Marketplace - JavaScript ES6 code snippets.
Publisher - charalampos karypidis. VS Code comes with many built-in code snippets. Please use ide. Load Comments. What's New. Most popular in JavaScript. More related articles in JavaScript. How do you run JavaScript script through the Terminal? JavaScript console. We use cookies to ensure you have the best browsing experience on our website. Start Your Coding Journey Now!
0コメント