Introduction
The path.extname function helps you find the file extension from a file name or path. It makes it easy to know what type of file you are working with.
Jump into concepts and practice - no test required
path.extname(pathString)
path.extname('photo.jpg') // returns '.jpg'
path.extname('/folder/file.tar.gz') // returns '.gz'
path.extname('README') // returns ''
path.extname('.gitignore') // returns ''
import path from 'path'; const files = ['index.html', 'photo.jpg', 'archive.tar.gz', 'README', '.env']; files.forEach(file => { const ext = path.extname(file); console.log(`File: ${file} - Extension: '${ext}'`); });
path.extname('example.txt') return in Node.js?path.extname method extracts the file extension including the dot from a filename string.path module?path module provides the method extname to get file extensions.path.extname('filename'). Other options are invalid method names.const path = require('path');
console.log(path.extname('archive.tar.gz'));path.extname method returns the substring from the last dot to the end of the string.const path = require('path');
const ext = path.extname('document');
console.log(ext);path.extname returns an empty string when no extension is found, not an error or undefined.path.extname to do this?const path = require('path');
const files = ['app.js', 'index.html', 'script.ts', 'readme'];
const jsFiles = files.filter(???);
console.log(jsFiles);path.extname(file) === '.js'. Other options either miss the dot, use wrong case, or check for empty extension.