Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Using path.extname to Extract File Extensions in Node.js
📖 Scenario: You are building a simple Node.js script to help organize files by their extensions. To do this, you need to extract the file extension from each filename.
🎯 Goal: Create a Node.js script that uses path.extname to get the file extension from a list of filenames.
📋 What You'll Learn
Create an array called files with the exact filenames: 'report.pdf', 'photo.jpg', 'archive.zip', 'notes.txt'
Create a variable called extToFind and set it to '.jpg'
Use a for loop with the variable file to iterate over files and create an array extensions that stores the extension of each file using path.extname(file)
Add a final line that exports the extensions array using module.exports = extensions
💡 Why This Matters
🌍 Real World
Extracting file extensions is useful when organizing files, validating uploads, or processing files differently based on type.
💼 Career
Understanding how to use Node.js built-in modules like path and how to manipulate arrays is essential for backend JavaScript development.
Progress0 / 4 steps
1
Create the list of filenames
Create an array called files with these exact filenames: 'report.pdf', 'photo.jpg', 'archive.zip', 'notes.txt'
Node.js
Hint
Use square brackets [] to create an array and separate items with commas.
2
Add a variable for the extension to find
Create a variable called extToFind and set it to the string '.jpg'
Node.js
Hint
Use const to declare a variable and assign the string '.jpg'.
3
Extract extensions using path.extname in a loop
Use a for loop with the variable file to iterate over files. Inside the loop, use path.extname(file) to get the extension and store all extensions in an array called extensions.
Node.js
Hint
Remember to require('path') at the top. Use extensions.push(path.extname(file)) inside the loop.
4
Export the extensions array
Add a final line that exports the extensions array using module.exports = extensions
Node.js
Hint
Use module.exports = extensions to make the array available outside this file.
Practice
(1/5)
1. What does path.extname('example.txt') return in Node.js?
easy
A. '.txt'
B. 'txt'
C. 'example.txt'
D. '' (empty string)
Solution
Step 1: Understand what path.extname does
The path.extname method extracts the file extension including the dot from a filename string.
Step 2: Apply to 'example.txt'
Since the file name is 'example.txt', the extension is '.txt' including the dot.
Final Answer:
'.txt' -> Option A
Quick Check:
Extension includes dot = '.txt' [OK]
Hint: Remember extname returns extension with dot [OK]
Common Mistakes:
Forgetting the dot in the extension
Returning the whole filename
Returning empty string for files with extension
2. Which of the following is the correct syntax to get the extension of a file named 'photo.jpeg' using Node.js path module?
easy
A. path.extname('photo.jpeg')
B. path.extension('photo.jpeg')
C. path.getExt('photo.jpeg')
D. path.ext('photo.jpeg')
Solution
Step 1: Recall the correct method name
The Node.js path module provides the method extname to get file extensions.
Step 2: Check the syntax
The correct syntax is path.extname('filename'). Other options are invalid method names.
Final Answer:
path.extname('photo.jpeg') -> Option A
Quick Check:
Correct method is extname() [OK]
Hint: Use path.extname() exactly as named [OK]
Common Mistakes:
Using incorrect method names like extension or ext