0
0
Node.jsframework~10 mins

path.extname for file extensions in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - path.extname for file extensions
Input: file path string
Find last '.' in string
If '.' found and not last char
Extract substring from '.' to end
Return file extension
The function takes a file path string, finds the last dot, and returns the substring from that dot to the end as the file extension. If no dot or dot is last, it returns an empty string.
Execution Sample
Node.js
const path = require('path');
const ext = path.extname('folder/file.txt');
console.log(ext);
This code extracts the file extension from 'folder/file.txt' and prints it.
Execution Table
StepInput StringActionResultOutput
1'folder/file.txt'Find last '.'Index 11
2Index 11Check if '.' is last charNo, last char index 14
3Index 11Extract substring from '.' to end'.txt'
4'.txt'Return extension'.txt''.txt'
5'folder/file'Find last '.'No '.' found
6No '.'Return empty string''''
7'file.'Find last '.'Index 4
8Index 4Check if '.' is last charYes, last char index 4
9'.' at endReturn empty string''''
💡 Execution stops after returning the extension or empty string depending on input.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4After Step 6After Step 9
inputString'folder/file.txt''folder/file.txt''folder/file.txt''folder/file.txt''folder/file''file.'
lastDotIndexundefined111111-14
extension'''''.txt''.txt'''''
Key Moments - 2 Insights
Why does path.extname return an empty string when the file ends with a dot?
Because the function checks if the dot is the last character (see Step 8). If yes, it returns an empty string since no extension follows the dot.
What happens if the input string has no dot at all?
The function finds no dot (Step 5), so it returns an empty string (Step 6). This means no extension is present.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at Step 4 for input 'folder/file.txt'?
A'.txt'
B'txt'
C''
D'.file'
💡 Hint
Check the 'Output' column at Step 4 in the execution_table.
At which step does path.extname return an empty string for input 'folder/file'?
AStep 3
BStep 9
CStep 6
DStep 1
💡 Hint
Look for the step where no '.' is found and empty string is returned.
If the input is 'file.', what does path.extname return according to the table?
A'.file'
B''
C'.'
D'file'
💡 Hint
See Steps 7-9 where the dot is last character and empty string is returned.
Concept Snapshot
path.extname(pathString)
- Finds last '.' in pathString
- Returns substring from '.' to end as extension
- Returns '' if no '.' or '.' is last char
- Useful to get file extensions
- Example: path.extname('file.txt') returns '.txt'
Full Transcript
The path.extname function takes a file path string and finds the last dot character. If a dot exists and is not the last character, it extracts and returns the substring from that dot to the end, which is the file extension. If no dot is found or the dot is the last character, it returns an empty string. For example, 'folder/file.txt' returns '.txt', 'folder/file' returns '', and 'file.' returns ''. This helps identify file extensions safely.