get extension from filename nodejs code example

Example 1: how to check file extension in node js

var path = require('path');

var ext = path.extname('/Users/Refsnes/demo_path.js');

  console.log(ext);

Example 2: how to get the extension from filename using javascript

var ext = fileName.substr(fileName.lastIndexOf('.') + 1);

Example 3: how to read file extension in a folder in node js

fs.readdir('./', (err, files) => {
        files.forEach(file => {
        //   console.log(file);
         // read extension
          const extension = file.split(".").pop();
          });
      })