
In my previous blog, we looked at the properties of JavaScript and their (dis)advantages. We saw that JavaScript is easy to read, allows for compact, flexible code and improves maintainability and development time. However, it is runtime inefficient, and troubleshooting is hard, which can be a pain the ass, indeed. So why are programmers using this language so much, and what can you actually do with it? Lets look at a few advantages of using JavaScript, focussing on the web and app development branch.
Interactivity
Interactivity is the first thing that comes to mind when hearing the word JavaScript, for us dummies at least 😊. Interaction appears to be linked to credibility; people tend to believe an interactive website more than they would believe a static one. JavaScript can be used to do this. We can show a little animation when a user clicks a button, or add some transition effects when the user scrolls the page, just to give your website or application that little bit extra. Besides this, interactivity can also be used to attract attention to a specific part of the website, which makes the page easier to use and improves the user experience.
So, interaction is clearly a good thing to build into a website, but why use JavaScript for this, risking many unexpected bugs and inefficient runtime? As you might know, a web page is built with HTML, the skeleton of the page. This skeleton can be dressed using CSS; which means styling the page statically. JavaScript can then be used to move the skeleton, and change or animate CSS properties; making the web page interactive. JavaScript is used as a client-side language, which means that the code is written into an HTML page by the browser (it runs scripts on your computer after you have loaded a web page). So, the script can interact with the HTML of the website by selecting elements of it and then manipulate those to provide the desired interactivity.
Why can we not use a server-side language to create interactive web pages? A server-side (or back-end) language runs its scripts before the HTML loads, not after. It is used for processing user input (like forms), structuring web pages and talking to the database. However, it cannot be used to create an interactive website. Therefore, client-side languages are used for interactivity. But there are more client-side languages besides JavaScript, like FLASH and Silverlight. So why would we use JavaScript? JavaScript is compatible with most browsers compared to other client-side languages, which is especially important when designing mobile websites and apps. Also, JavaScript is easier to use because it is fully integrated with CSS and HTML. Since JavaScript is so popular, many libraries and frameworks are available for this language (like jQuery and React). This is another important reason for using JavaScript instead of an alternative language.

Faster web page
As discussed in the previous blog, JavaScript decreases runtime efficiency due to dynamic programming (executing tasks at runtime). However, JavaScript can also be used to make a web page faster. How can this possibly be, you might think. Well, there are various reasons for this. For example, JavaScript enables us to load new images, objects or scripts without the need to reload the entire web page. Imagine yourself redecorating your entire Christmas tree every time you just want to add another ornament. This would be very tedious, would it not? Well, this can be avoided by using of JavaScript. Also, JavaScript can be used to determine in which order elements of a page are loaded. Instead of keeping the user waiting until the entire page is loaded, we can load the page’s elements piece by piece, reducing waiting times for the user and optimizing the user experience.
JavaScript is weakly typed, which allows us to write code reasonably easily, without discipline or controls. However, this will reduce the performance of your app or website. To minimize the adverse influence of JavaScript on the runtime and only enjoy its positive effect, we can optimise JavaScript code in various ways. I will not discuss it here, but this website gives quite a good overview of what you can do to optimise performance.
Client-side validation
A significant reason for using JavaScript is fast, client-side validation. Imagine you want to order a Christmas sweater online, and you need to create an account to do so. You need to fill in a form with your name, address and phone number which is sent to the database. Without JavaScript, the form you filled in is sent to the server once you submit it. The server checks if you filled in everything correctly and if not, you are asked to repeat the entire process. This can get very annoying, especially if a delicious cup of hot chocolate and a Christmas cookie are waiting for you. Would it not be great to check every single input item at the client side, before sending the entire form back to the server? Well, JavaScript can do exactly this. It validates every input field while you are still filling in the form and you get feedback right away. This prevents you from having to fill in the form twice because of a typo in the first input field, which makes it way easier for you to order that great Christmas sweater. The question now is, is it possible to do this with another language besides JavaScript? Well, HTML5 allows for a little validation as well. It can check whether a required field is empty or not, whether the input matches a required pattern or a required length. However, HTML5 is not supported by all browsers, and you have to use the default look and feel of error messages. If you want customised messages or check for more specific criteria, you have to use JavaScript. An important note here: client-side validation is a convenience for the user, but server-side validation is essential. So, you should never use client-side validation only, but use both client-side and server-side validation to retrieve the correct data. Check, check, double check!
Building web and mobile apps all with the same language.
Another huge advantage of using JavaScript comes to mind when developing mobile apps. JavaScript can be used to develop an app for both Android and iOS simultaneously. This means that development efficiency (and therefore cost-efficiency as well) can be improved a lot, since the app has to be developed just once. Besides this, the use of JavaScript allows for a comfortable learning curve since it is a high-level language, as discussed in the previous blog. A native language is a language understood by the computer or processor, which is usually harder to understand than the JavaScript language.
If you want to use JavaScript for building an app, you need a platform to do so. A platform used a lot nowadays is React native. This is a platform solely based on JavaScript, and the code is deployable to both iOS and Android. However, you still have to write some code specifically for Android and iOS if you want to create the native feel of an app because React native does not support things like push notifications. So, JavaScript can be used to build the basic functionality of the app, and you can choose to optimise it by using a native language additionally, but you do not have to.
Conclusion
In conclusion, the use of JavaScript has various advantages. First of all, it allows for creating a fast and interactive website, which improves the user experience. Another factor that influences the way a user perceives the website or app is client-side validation. Giving feedback on the go prevents users from getting irritated and so they create a more positive feeling towards your website or app. Lastly, a significant advantage of JavaScript is that it can be used to develop apps for both Android and iOS, which improves time- and cost-efficiency.
So, once you familiarise yourself with the JavaScript language, you can build interactive, user-friendly apps and websites!
