skip to Main Content

I am trying to assign path to a field in an object using NodeJS & Javascript.

Below is the code that I am using:

console.log("__dirname ===> ", __dirname);
let direc = __dirname;
console.log("direc : ", direct);
let p = {};
p.da = __dirname;
console.log("p: ", p);

Its output is:

__dirname ===> C:Practicetestingpracticelessonsone
direc : C:Practicetestingpracticelessonsone
p:{ da: "C:\Practice\testingpractice\lessons\one" }

So, above as you can see, I have printed __dirname which gives correct path name. Have also assigned __dirname to a new variable and printed. That did not give 2 slashes. But when I assign it to a field of an object there are 2 slashes. In this case it is p.da which gave C:\Practice\testingpractice\lessons\one

Output that I expect is :

p:{ da: "C:Practicetestingpracticelessonsone" }

I do not expect 2 slashes. I want it a pure path name of the directory. And I want it in that manner only. Not with /. Expectation is with . The steps and code I want is exact same as above.

I have also tried with regex. But did not seem to work also.

Below is the code for it:

console.log("__dirname ===> ", __dirname);
let direc = __dirname;
console.log("direc : ", direct);
var regex = /\/g;
let p = {};
p.da = __dirname.replace(regex,'\');
console.log("p: ", p);

Gave below output:

__dirname ===> C:Practicetestingpracticelessonsone
direc : C:Practicetestingpracticelessonsone
p:{ da: "C:\Practice\testingpractice\lessons\one" }

Both the outputs were same.

Tried to compare the strings, but did not return as equal

if(direc === p.da) {console.log("equal");} else {console.log("not equal");}

Output:
not equal

I don’t know where I am going wrong. Please help me understand my mistake if any and appreciate for the solutions. Thanks in advance!

2

Answers


  1. console.log formats strings with " marks around them and escape characters in the data.

    If you don’t want that, write your own output function using process.stdout.write (or stderr).

    Login or Signup to reply.
  2. There is nothing wrong. The string only has one slash. What you are seeing is just how string syntax work in javascript and JSON.

    This string:

    "C:\Practice\testingpractice\lessons\one"
    

    Have only one not two slashes in the path.

    This string:

    "C:\\Practice\\testingpractice\\lessons\\one"
    

    have two slashes in the path.

    Therefore what you see is correct and there is nothing you need to do except to stop worrying.

    In javascript strings (and many other languages) the backslash character () has special meaning. It is used to signal to the language that you are inserting a special character into the string.

    For example, n represents the newline character and t represents the tab character. So for example, the following string:

    "Hot sign!"
    

    Actually looks like this:

    Ho     sig
    !
    

    So you cannot use in a string to represent the backslash character because the language interprets it as a special character. To solve this problem the string syntax allows you to use \ to represent one backslash character.

    The following string:

    "C:Practicetestingpracticelessonsone"
    

    Actually looks like this:

    C:Practice    estingpracticelessonsone
    

    Since t is the tab character and any sequence (combination of and other characters) that does not have any special meaning is replaced with the escaped character. For example P is just the letter P.

    You can test this by trying out the following:

            if ("C:Practicetestingpracticelessonsone" === "C:Practice   estingpracticelessonsone") {
                console.log("It is true!");
            }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search