Salesforce Certified JavaScript Developer free question 9 / 10 · Domain 6: Server Side JavaScript
On a POSIX system, a developer resolves a list of path segments. path.resolve('/foo', '/bar', 'baz'); What does this call return?
- /foo/baz, because the last absolute segment is replaced by the first one
- /foo/bar/baz, because the segments are appended to the previous one
- baz, because the last segment is the file name and earlier segments are ignored
- /bar/baz, because processing from right to left stops at /bar
Show the answer and explanation
Answer: D
Why this is correct
path.resolve processes the sequence from right to left, prepending each earlier segment until an absolute path is constructed. baz is not absolute, but /bar + / + baz is, so the result is /bar/baz and /foo is never used.
Why the other options are wrong
"/foo/bar/baz, because the segments are appended to the previous one" … path.resolve processes the segments from right to left and stops once an absolute path is constructed, so /foo is not used.
"/foo/baz, because the last absolute segment is replaced by the first one" … The segments are processed from right to left, so the later absolute path /bar is the one that is kept, followed by baz.
"baz, because the last segment is the file name and earlier segments are ignored" … path.resolve returns an absolute path. Since baz is not absolute, it is prepended with /bar.
Key points
A later absolute segment discards everything before it. path.join, by contrast, only concatenates and normalizes.