0

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:\Practice\testingpractice\lessons\one
direc : C:\Practice\testingpractice\lessons\one
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:\Practice\testingpractice\lessons\one" }

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:\Practice\testingpractice\lessons\one
direc : C:\Practice\testingpractice\lessons\one
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!

itiDi
  • 370
  • 3
  • 18
  • A possible duplicate would be [Why do backslashes appear twice?](https://stackoverflow.com/q/24085680) (or another question linked from there). It is about Python, but the same applies to JS as well. – InSync May 22 '23 at 14:10
  • @InSync This does not answer my question though. – itiDi May 22 '23 at 14:13
  • Your just seeing string literal escaping, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_escape IOW: 2 forward slashes = 1 forward slash internally, – Keith May 22 '23 at 14:15
  • To some extend, it does. Your string does not include any double backslashes. NodeJS prints them out as a part of the representation of that string. – InSync May 22 '23 at 14:15
  • @InSync I have printed `__dirname` at first which gave correct path name. But when I assigned it to an object, it gave 2 slashes. – itiDi May 22 '23 at 14:27
  • @itiDi Your confusing strings and object here, the strings are identical, but how an object is represented in the console, and how an object is shown are totally different. if you did -> `console.log(p.a === __dirname)` you will see it equals `true`, and like I pointed out earlier all your seeing is string escaping that is used when representing strings inside objects. – Keith May 22 '23 at 14:32
  • No. I tried to print in if-else condition. It did not return true. – itiDi May 23 '23 at 04:58

2 Answers2

0

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).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

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:

"Ho\t sig\n!"

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:\Practice\testingpractice\lessons\one"

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:\Practice\testingpractice\lessons\one" === "C:Practice   estingpracticelessonsone") {
            console.log("It is true!");
        }
slebetman
  • 109,858
  • 19
  • 140
  • 171
  • Okay sure @slebetman I will go through this answer and your suggestions. Thank you. I kind off understood it now. – itiDi May 23 '23 at 08:48