Should beginners learn JavaScript?

JavaScript is a widely-used programming language that is easily accessible and often the first language people learn. While it was not my initial programming language, I began using it, off and on, over 10 years ago. Initially, I struggled to understand JavaScript as it operates differently from the other programming languages I had encountered before it. Though this difference may not be immediately obvious to those who are new to programming. Many beginners share a common misunderstanding of the nature of the language, and this may be why JavaScript is viewed with such polarized opinions among developers. I used to dislike it myself and only managed to write basic code with the help of frameworks like jQuery, but I realized that I hadn’t taken the time to truly understand the language. Now that I have a better grasp of its core concepts, I enjoy coding with JavaScript. I believe that anyone can come to appreciate the language if they take the time to understand its fundamentals.

Regarding the question of whether JavaScript is a good language for beginners, I believe it is not the most ideal option. If there is a need to learn the fundamentals of programming, one should consider exploring other options, such as Python. In my experience, JavaScript can be a bit quirky and misleading, even though its syntax appears similar to other languages. Furthermore, its approach to Object-Oriented Programming is prototypal, which differs from most other OOP languages that are class-based. This difference can lead to unexpected obstacles for those who are not familiar with the distinctions. As a result, I generally recommend class-based languages as a more intuitive option for beginners to program with.

To begin to understand the language there are two main aspects that JS novices should grasp.

The execution environment

Developers need to understand the single-threaded characteristics of the JS engine and how it works within the environment (either a browser or server) and its accompanying APIs. This knowledge is useful for understanding an application’s control flow, or in other words, the order in which the application executes. It’s important to note that in JS and event-driven programming, the code doesn’t always execute step-by-step, as asynchronous functions are heavily relied upon for certain tasks. These functions execute outside of the main control flow and return a result only when the function has finished. While there are resources available online that cover this topic in more detail, I won’t be delving further into it here.

Inheritance

Developers need to grasp the distinction between prototypal and class-based inheritance. Inheritance allows for the transfer of properties and behaviours from one thing to another. Learning how JS manages inheritance, even at a basic level, is crucial for beginners to this language.

Let’s take the example of a clock. A clock displays time using seconds, minutes, and hours. Analogue and digital clocks derive from the idea of a clock. We can further refine these types of clocks. We can have digital wristwatches and digital wall clocks. This concept is known as inheritance, where traits and properties are inherited from one class of objects to another.

Class-based

When using class-based languages for inheritance, we start by defining the classification of a Clock. From there, we can create a new classification called Digital Clock, where we describe its properties and behaviours. This classification can then be further extended to create Digital Wristwatch. Digital Wristwatch inherits properties and behaviours from Digital Clock, which in turn inherits from Clock. This creates a blueprint for Digital Wristwatch, which can be used to create new objects. In summary, class-based inheritance results in a blueprint through which a new object can be created.

Prototypal

To understand how prototypal inheritance works, we start by creating a base object called “Clock” and assigning it properties and behaviours. We then specify which of these can be passed on to other objects. For instance, if we want to create a “Digital Clock” object, we first create a simple object with that name. To enable it to use the behaviours of the “Clock” object, we link the “Clock” to the “Digital Clock’s” prototype. We can then add unique properties and behaviours to the “Digital Clock” object. Similarly, we can create a “Digital Wristwatch” object and link it to the “Digital Clock” object, forming a chain of inheritance. This data structure is called a “Prototype Chain” in JavaScript and is essential to understanding the language. In summary, prototype inheritance involves creating an object and linking it to another object, with the top object in the chain appearing to inherit the properties and behaviours of the linked objects below it.

How JS implements prototypal inheritance is a little more involved than the example given, but the example sums up most of it.

Conclusion

For beginners, it’s important to understand that JS is a classless language. This may be confusing when seeing the word “class” being used to create new objects in modern JS syntax, along with the keyword “extends” for inheritance class inheritance. However, these are just syntactic features that make it easier for those familiar with class-based languages to learn JS. It’s worth noting that JS can be deceptive, as the syntax doesn’t always reflect what’s going on underneath its prototypal hood. This can lead to confusion, which is why I believe that JS may not be the best language for beginners.

Revised: 3rd May 2023