Start learning free

LESSON 78 OF 671 · STRINGS - CHALLENGES

File Extension in JavaScript: split(".")

Practice Read the full explanation

Given a file name fileName, return its extension: the part of the name after the last dot.

Split the name at every dot, and the extension is the last of the parts. A name can have more than one dot, like "archive.tar.gz": only what comes after the last dot is the extension. Keep its letters exactly as they are.

 

Example 1:

Input: fileName = "photo.jpeg"
Output: "jpeg"
Explanation: The name has one dot, and "jpeg" comes after it.

Example 2:

Input: fileName = "archive.tar.gz"
Output: "gz"
Explanation: Splitting at the dots gives "archive", "tar" and "gz". The last part, "gz", is the extension.

Example 3:

Input: fileName = "README.MD"
Output: "MD"
Explanation: The extension keeps its upper-case letters.

 

Constraints:

  • fileName has at least one dot, and doesn't start or end with a dot.
  • fileName has no spaces: only English letters, digits, dots, - and _.
STEP 1 OF 2
    Ctrl + Enter to check

    JAVASCRIPT · STRINGS - CHALLENGES

    File Extension in JavaScript, explained

    Problem overview

    The method fileExtension receives a file name, fileName, and returns its extension: the part after the last dot. The name has at least one dot and neither starts nor ends with one.

    "photo.jpeg" gives "jpeg". "archive.tar.gz" has two dots, and only what follows the last one counts, so the answer is "gz". "README.MD" gives "MD": the letters keep their case.

    Approach

    The method split(".") cuts a string at every dot and returns an array of the pieces. "archive.tar.gz".split(".") is ["archive", "tar", "gz"]. The extension is always the last piece, however many dots the name has.

    An array is numbered from 0 and has a length, just like a string, so its last item is at index parts.length - 1.

    Step by step

    1. Split the name at the dots into an array called parts.
    2. Return the last item of parts, at index parts.length - 1.

    What split returns

    For "photo.jpeg", split(".") returns ["photo", "jpeg"], an array of length 2, so the last index is 1. For "archive.tar.gz" it returns ["archive", "tar", "gz"], an array of length 3, so the last index is 2. The formula parts.length - 1 adapts to the number of dots, which a fixed index can't do.

    The solution in JavaScript

    class Solution {
        fileExtension(fileName) {
            let parts = fileName.split(".");
            return parts[parts.length - 1];
        }
    }
    

    On the examples it returns:

    "photo.jpeg"     -> "jpeg"
    "archive.tar.gz" -> "gz"
    "README.MD"      -> "MD"
    

    The dot passed to split is a plain string, not a pattern, so it matches a literal dot. Splitting never changes the letters of the pieces.

    Another way

    You can also find the last dot directly. lastIndexOf(".") returns the index of the last dot, and slice with the index after it returns the rest: for "archive.tar.gz", lastIndexOf(".") is 11 and fileName.slice(11 + 1) is "gz". This avoids building an array. The lesson uses split, which also shows how arrays are indexed.

    Thinking about the problem

    The statement defines the extension as the part after the last dot. A name can have several dots, so the first dot is not the one that matters. Cutting at every dot turns the name into pieces, and the extension is the final piece. The name never starts or ends with a dot, so no piece is empty: "archive.tar.gz" never produces an empty item at either end. That is why the last piece is always a real extension.

    Edge cases and common mistakes

    • Taking parts[1]. That works for "photo.jpeg" but returns "tar" for "archive.tar.gz". Use the last index.
    • Using parts[parts.length]. That is one past the end and gives undefined.
    • A name without a dot. "noext".split(".") is ["noext"], so the code would return the whole name. The lesson guarantees at least one dot.
    • Changing the case. The extension is returned exactly as written.

    Testing

    • "my-file_1.v2.txt" gives "txt": hyphens, underscores and digits stay inside their parts.
    • "a.b" gives "b": a one-letter extension is fine.

    An array method such as at(-1) also reads the last item: "a.b".split(".").at(-1) is "b". The lesson uses the index parts.length - 1, which works the same way and is the form you will meet in older code.

    Keep going with JavaScript650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free