
Our instructor, Tom, is not a fan of JavaScript. He’s a C# guy. But this week he has found himself teaching a room with 16 eager programmers bent on warding off the anxiety of the job search with a few hours of coding. We want to learn. We’re ready for the distraction.
Though Tom doesn’t often use JavaScript in his professional life, he knows it inside and out. And if he stumbles across an issue he doesn’t understand, it’s solved within seconds. But there is an element of exasperation in his teaching tone as he discusses some of the rules of JavaScript and their differences with his beloved C#. He refers to many of these differences as “Black Magic”. Mysterious, chaotic code which yields dubious outcomes.
It’s funny. It gets a laugh every time.
C# is can be called a “strongly typed” language. You have to declare a number as an “int” before you use it in a variable, you must declare text as a “string”. Not so with JavaScript. Make a variable and it can be whatever. Yes, no, true false, 5, banana. Doesn’t matter. JavaScript doesn’t care (another Tomism on the subject of JS).
Whereas in C#, you must tell your methods exactly what variable types they should be expecting, JavaScript will deal with anything you throw it. How does it deal with it? Black magic. It will assume certain elements. Used correctly, its an easier language to learn. When things get complicated though, it will often end up doing things you might not have intended.
For example if you pass two parameters and you think they are 2 and 5 and you want to add them together, you might expect to get 7. However, if the parameters happen to be strings and you didn’t know, it will concatenate them instead of add them and you will end up with 25. Poof! You’ve been black magicked!
There are certainly some advantages. I mean, it’s the main language that programmers write front-end in, so there must be something to it. And also, it’s the first thing I learned in my programming journey. I studied JavaScript just after HTML and CSS when I was prepping for TE. So it has a soft spot in my heart.
Just like anything else, the more I study it, the more I will like it. Black Magic upon closer inspection will often yield good outcomes. We just have to be more diligent wizards.