Blogs like this one mostly cover different areas of IT as an industry and as a profession: project management, building good architecture and, in general, the factors that improve the whole software development process. Sometimes, though, it's worth going back to the basics, the really basic basics, which concern not only programming but simply using a computer. As professionals or teachers, whether in industry or academia, we sometimes underestimate these topics and take them for granted. Yet it is often fluency with such small things that makes the difference in productivity and ease of working in IT, the difference newcomers lack. That's why it is sometimes worth writing about them.

Today we'll look at the easiest way to learn the difference between the two slashes we see on our keyboards: the slash (/) and the backslash (\).

As I said, the topic may seem odd, and for many people the difference is intuitive. But using the right slash is essential, for example when writing scripts that work with files. Typing the wrong character usually doesn't end in tragedy, but it can be discouraging (when you have to fix the code in many places) and lead to needless irritation and wasted time. So let's go through the simplest ways to remember which slash is which.

The visual difference

Let's start with one fundamental thing: the two characters look very similar, but they lean in different directions. The slash (/) leans to the right, while the backslash (\) leans to the left. You can picture a person who, like Michael Jackson, stands leaning in a given direction and associate that with the character. But there are better ways to remember the difference.

Path separator in the operating system

Even inexperienced computer users know that when you move through folders on a disk, you build a path out of them that leads to a specific directory, however deeply nested. In structure it is similar to a web address (which we'll get to today as well), only it refers to our local set of data. The individual folders in a path are separated by slashes, but which kind depends on the system.

For Linux and other Unix-like systems it is the slash. For Windows it is the backslash, which goes back to a decision made long ago, when this character was meant to help preserve compatibility between early versions of DOS. In DOS 1.0 the slash was used for command options, so DOS 2.0, when it introduced the directory structure, had to adopt the "\" character. And that's how it has stayed to this day.

Interestingly, in PHP we don't have to think about which separator to use or how to tell them apart when handling files on different operating systems. We just use the DIRECTORY_SEPARATOR constant.

On the keyboard

On most keyboards (and perhaps all of them) the slash is lower, usually to the left of the right Shift key. The backslash is always higher, usually above ENTER. On older layouts you could also find it to the left of ENTER, but that is still higher than the "forward slash".

A map of Poland

This is my favorite way to tell them apart, although I didn't come up with it. The idea came from a friend from my computer science studies (whom I warmly greet), and it works surprisingly well.

Let's look at the slash and the backslash once more and return to the visual difference between them: the first leans to the right (that is, its top points that way), the second to the left. Now look at a map of Poland and the cities in the upper right and upper left corners. The right corner takes us to the Podlaskie voivodeship and Suwałki, known even from weather maps (although Sejny fits too). Notice that this city starts with the letter "S", just like "slash", right? Below is a Google Maps screenshot to confirm.

Suwałki as the counterpart of the slash on a map of Poland

Following this lead, in the upper left corner of Poland (the West Pomeranian voivodeship) we should look for a city starting with "B", like "backslash". And indeed we find one, although a bit farther from the border: Białogard. So we can remember that the slash leans toward Suwałki, and the backslash toward Białogard. Here is the Google Maps screenshot again:

Białogard as the counterpart of the backslash on a map of Poland

Of course, I send my greetings to the residents of all the cities mentioned.

Other differences

We now know how to identify both characters: by their link to the operating system, their place on the keyboard and their place on the map of Poland. Let's move on to other situations (or at least some of them) where each symbol is used.

Slash

  • I already mentioned that this is the character used in web addresses. For example, in the address of the page where you can suggest topics for the blog, we see slashes both after the protocol and between the subsequent parts of the URL.
  • Slashes very often start commands in various programs. To name just a few: that's how we search for phrases in Vim, run plugins in Slack, control IRC, and even choose the target group in the chat of Valorant.
  • It is used as the division operator in computer notation.

Backslash

  • It is used as an escape character. When we want to output (in code or in a regular expression) a character that normally has another purpose, we use the escape character, which in a way "cancels" the special interpretation of the character that follows. For example, if we want to match a literal dot rather than use it as a stand-in for any other character, we write "\.". Note that the backslash itself is a special character, so to include it in a path in code (in Java, for example), we have to write "\\".
  • In JavaScript it lets you create a string that spans multiple lines of text.
  • In PHP it lets you use multi-level namespaces.

Summary

As you can see, all these characters exist for a purpose and have their own meaning. If you still think you might mix them up and will never learn the difference, don't panic. In this case the best teacher is simply practice and thinking about which character to use in a given situation. Over time their properties will "jump" into your head on their own, and you won't even know when it happened.

Best regards and thank you, Jakub Rojek.