-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path63. Get_file_extension.js
More file actions
26 lines (21 loc) · 882 Bytes
/
63. Get_file_extension.js
File metadata and controls
26 lines (21 loc) · 882 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//*----- Get file extension
//? To get the file extension from a string repersenting a file name in javascript,you can use the various methods. One common approach is to use the split() method to split the file name based on the dot(.) and then extract the last part of the resulting array.
function getFileExtension(fileName) {
//? Split the file name based on the dot(.) and then extract the last part of the resulting array
//* method 1
let fileExtension = fileName.split(".").pop();
return fileExtension;
//* method 2
let fileSplit = fileName.split(".");
console.log(fileSplit);
let extensionPart = fileSplit.at(-1);
console.log(part);
return extensionPart;
//* method 3
let parts = fileName.split(".");
let extension = parts[parts.length - 1];
return extension;
}
//? Example usage
let file = getFileExtension("index.html");
console.log(file);