find the name of a file in javascript

In order to find the name of a file in JavaScript, you can use the split() method. First, you need to get the full path of the file as a string. For example:

index.tsx
var filePath = "/path/to/file/example.txt";
44 chars
2 lines

Then, you can split the string by the forward slash character ("/"), which will give you an array of strings:

index.tsx
var array = filePath.split("/");
33 chars
2 lines

The last element of the array will be the file name:

index.tsx
var fileName = array[array.length - 1];
40 chars
2 lines

Now, the fileName variable will contain the name of the file, which in this case is "example.txt".

gistlibby LogSnag