Open In App

Node.js path.isAbsolute() Method

The path.isAbsolute() method is used to check whether the given path is an absolute path or not. An absolute path is defined as a path that contains the complete details needed to locate a file.

Syntax:



path.isAbsolute( path )

Parameters: This method accepts single parameter path which holds the file path that would be used to check if it is an absolute path. A TypeError is thrown if this parameter is not a string.

Return Value: It returns a Boolean value indicating whether the path is an absolute path. It returns ‘false’ if the path is of zero-length.



Below programs illustrate the path.isAbsolute() method in node.js:

Example 1:




// Node.js program to demonstrate the   
// path.isAbsolute() method
  
// Import the path module
const path = require('path');
   
path1 = path.isAbsolute("/user/bash/");
console.log(path1);
   
path2 = path.isAbsolute("user/bash/readme.md");
console.log(path2);
   
path3 = path.isAbsolute("/user/bash/readme.md");
console.log(path3);
   
path4 = path.isAbsolute("..");
console.log(path4);

Output:

true
false
true
false

Example 2:




// Node.js program to demonstrate the   
// path.isAbsolute() method
  
// Import the path module
const path = require('path');
   
path1 = path.isAbsolute("\\user\\bash\\");
console.log(path1);
   
path2 = path.isAbsolute("user\\bash\\readme.md");
console.log(path2);
   
path3 = path.isAbsolute("\\user\\bash\\readme.md");
console.log(path3);
   
path4 = path.isAbsolute("..");
console.log(path4);

Output:

true
false
true
false

Reference: https://nodejs.org/api/path.html#path_path_isabsolute_path


Article Tags :