find the extension of a file in javascript

To find the extension of a file in Javascript, you can first get the name of the file and then find the last index of the "." character to extract the extension.

Here's an example code snippet:

index.tsx
const fileName = "example.html";
const fileExtension = fileName.slice(fileName.lastIndexOf(".") + 1);
console.log(fileExtension); // Output: "html"
148 chars
4 lines

In the above example, we first defined the fileName variable with the name of the file. Then, we used the slice method to extract the substring after the last occurrence of the "." character in the file name, which gives us the file extension.

Finally, we logged the value of fileExtension to the console.

related categories

gistlibby LogSnag