path.basename?import path from 'path'; const filePath = '/home/user/docs/letter.txt'; console.log(path.basename(filePath, '.txt'));
The path.basename function returns the last part of a path. When you provide a second argument, it removes that extension from the result if it matches. Here, '.txt' is removed from 'letter.txt', so the output is 'letter'.
path.dirname?import path from 'path'; const filePath = '/var/www/html/index.html'; console.log(path.dirname(filePath));
path.dirname returns the directory part of the path, excluding the file name.
The path.dirname function returns everything before the last slash in the path. Here, it returns '/var/www/html' because 'index.html' is the file name.
path.basename on a path with a trailing slash?import path from 'path'; const dirPath = '/usr/local/bin/'; console.log(path.basename(dirPath));
The path.basename function returns the last portion of a path, ignoring trailing slashes. Here, '/usr/local/bin/' ends with a slash, but path.basename ignores it and returns 'bin'.
path.dirname in Node.js ES modules?Option C is missing a closing parenthesis in the console.log call, causing a syntax error.
path.basename return an empty string?import path from 'path'; const p = '/folder/subfolder/'; console.log(path.basename(p));
path.basename returns an empty string if the path ends with a slash (/), treating it as a directory path with no trailing filename. Here, '/folder/subfolder/' ends with a slash, so it returns an empty string.