Bird
Raised Fist0
Node.jsframework~10 mins

path.extname for file extensions in Node.js - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the 'path' module in Node.js.

Node.js
const path = require('[1]');
Drag options to blanks, or click blank then click option'
Afs
Bhttp
Cpath
Durl
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fs' instead of 'path' to import the module.
Forgetting to use quotes around the module name.
2fill in blank
medium

Complete the code to get the file extension from the filename.

Node.js
const ext = path.[1]('example.txt');
Drag options to blanks, or click blank then click option'
Abasename
Bextname
Cdirname
Djoin
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'basename' which returns the filename, not the extension.
Using 'dirname' which returns the directory path.
3fill in blank
hard

Fix the error in the code to correctly get the extension of 'archive.tar.gz'.

Node.js
const extension = path.[1]('archive.tar.gz');
Drag options to blanks, or click blank then click option'
Aextname
Bbasename
Cparse
Dformat
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'basename' which returns the full filename.
Using 'parse' or 'format' which are for different purposes.
4fill in blank
hard

Fill both blanks to get the extension of a file path stored in variable 'filePath'.

Node.js
const ext = path.[1]([2]);
Drag options to blanks, or click blank then click option'
Aextname
BfilePath
Cbasename
Ddirname
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'basename' instead of 'extname' for the method.
Using a string literal instead of the variable name.
5fill in blank
hard

Fill all three blanks to extract the extension and check if it equals '.js'.

Node.js
const ext = path.[1](filename);
if (ext === [2]) {
  console.log('This is a JavaScript file.');
}
Drag options to blanks, or click blank then click option'
Aextname
B'.js'
C'.txt'
Dbasename
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'basename' instead of 'extname'.
Comparing to '.txt' instead of '.js'.

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

  1. Step 1: Understand what path.extname does

    The path.extname method extracts the file extension including the dot from a filename string.
  2. Step 2: Apply to 'example.txt'

    Since the file name is 'example.txt', the extension is '.txt' including the dot.
  3. Final Answer:

    '.txt' -> Option A
  4. 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

  1. Step 1: Recall the correct method name

    The Node.js path module provides the method extname to get file extensions.
  2. Step 2: Check the syntax

    The correct syntax is path.extname('filename'). Other options are invalid method names.
  3. Final Answer:

    path.extname('photo.jpeg') -> Option A
  4. 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
  • Missing parentheses after method name
  • Confusing with other path methods
3. What will be the output of the following code?
const path = require('path');
console.log(path.extname('archive.tar.gz'));
medium
A. '.tar.gz'
B. '' (empty string)
C. 'tar.gz'
D. '.gz'

Solution

  1. Step 1: Understand how extname handles multiple dots

    The path.extname method returns the substring from the last dot to the end of the string.
  2. Step 2: Apply to 'archive.tar.gz'

    The last dot is before 'gz', so the extension returned is '.gz'.
  3. Final Answer:

    '.gz' -> Option D
  4. Quick Check:

    Extension is from last dot = '.gz' [OK]
Hint: extname returns from last dot to end [OK]
Common Mistakes:
  • Assuming it returns multiple extensions
  • Returning empty string for multiple dots
  • Including the whole suffix after first dot
4. Identify the error in this code snippet:
const path = require('path');
const ext = path.extname('document');
console.log(ext);
medium
A. It returns 'document' as extension
B. It throws an error because 'document' has no extension
C. It returns an empty string because no extension exists
D. It returns undefined

Solution

  1. Step 1: Check the filename for extension

    The filename 'document' has no dot, so no extension exists.
  2. Step 2: Understand extname behavior with no extension

    path.extname returns an empty string when no extension is found, not an error or undefined.
  3. Final Answer:

    It returns an empty string because no extension exists -> Option C
  4. Quick Check:

    No dot means empty string [OK]
Hint: No dot means extname returns empty string [OK]
Common Mistakes:
  • Expecting an error for missing extension
  • Expecting undefined instead of empty string
  • Confusing filename with extension
5. You want to filter an array of filenames to only include '.js' files. Which code snippet correctly uses 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);
hard
A. file => path.extname(file) === ''
B. file => path.extname(file) === '.js'
C. file => path.extname(file) === '.JS'
D. file => path.extname(file) === 'js'

Solution

  1. Step 1: Understand the filter condition

    We want to keep files whose extension is exactly '.js' including the dot and case-sensitive.
  2. Step 2: Use path.extname correctly

    The correct comparison is path.extname(file) === '.js'. Other options either miss the dot, use wrong case, or check for empty extension.
  3. Final Answer:

    file => path.extname(file) === '.js' -> Option B
  4. Quick Check:

    Filter by exact '.js' extension [OK]
Hint: Compare extname result including dot and case [OK]
Common Mistakes:
  • Omitting the dot in extension comparison
  • Ignoring case sensitivity
  • Filtering by empty string instead of '.js'